我正在寻找一种使用来自两个单独(但平行)数组的图像和文本填充表格视图的方法。我一直试图从用所有可能的内容实例化的两个可变数组开始,对照模型对象的内容进行检查,删除“空白”条目,然后将剩下的内容复制到填充表格视图单元格的表格视图属性中。

(从中获取其初始值的person对象是传入的模型对象。)

    NSMutableArray *profileList = [@[person.facebook, person.twitter, person.pinterest, person.linkedIn, person.youTube, person.googlePlus, person.flickr] mutableCopy];
    NSMutableArray *buttonList = [@[@"btn-Facebook.png", @"btn-Twittter.png", @"btn-Pinterest.png", @"btn-LinkedIn.png", @"btn-YouTube.png", @"btn-Google+.png", @"btn-Flickr.png"] mutableCopy];
    NSMutableArray *indicesToRemove = [@[] mutableCopy];

    //    for (NSString *index in profileList) {
    //
    //    }
    int i = 0;
    for (NSString *indexContents in profileList) {
        if ([indexContents isEqual: @""]) {
        // [indicesToRemove addObject:[NSString stringWithFormat:@"%d", i]];
            [indicesToRemove addObject:indexContents];
        }
        i++;
    }
    for (NSString *count in indicesToRemove) {
        // [profileList removeObjectAtIndex:[count integerValue]];
        [profileList removeObject:count];
        // [buttonList removeObjectAtIndex:[count integerValue]];
    }

    self.socialMediaProfilesList = (NSArray *)profileList;
    self.socialMediaButtonsList = (NSArray *)buttonList;


这适用于profileList,但不适用于profileList。 (此后,在buttonList中,tableView:cellForRowAtIndexPath:可以很好地设置,但是cell.textLabel.text会引发异常。)

正如您可能从注释行中看到的那样,我尝试重构此方法以跟踪索引而不是目录,但是这样我什至无法加载该表。

似乎我会以所有错误的方式进行操作,但是我无法提出更好的方法。有任何想法吗?

最佳答案

好的,指针:)


而不是使用快速枚举,请使用普通的for循环(for(int i = 0; i 将所有索引存储在NSMutableIndexSet
通过在两个数组上调用removeObjectsAtIndexes:清除所有对象


编辑:或者,也可以反转for循环的顺序(int = COUNT-1; i> = 0; i--),然后直接从数组中删除对象。

关于objective-c - 如何从两个数组填充表 View 内容?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15235530/

10-12 00:21
查看更多