如何从两个数组填充表视图内容?

时间:2013-03-05 22:38:53

标签: objective-c c uitableview nsmutablearray nsarray

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

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

    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但不适用于buttonList。 (从以后开始,在tableView:cellForRowAtIndexPath:中,cell.textLabel.text设置正常但cell.imageView.image会引发异常。)

正如你可以从注释掉的行中看到的那样,我尝试重构这个方法来跟踪索引而不是内容,但是我甚至无法让表加载。

似乎我正在以错误的方式解决这个问题,但我无法想出更好的方法。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

好的,指针:)

  • 而不是使用快速枚举,使用普通的for循环(for(int i = 0; i< COUNT; i ++))
  • 将所有索引存储在NSMutableIndexSet
  • 使用removeObjectsAtIndexes:清除所有对象,方法是在两个阵列上调用它们

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