0

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

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. (因为后来, in tableView:cellForRowAtIndexPath:cell.textLabel.text设置好但cell.imageView.image抛出异常。)

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

似乎我正在以错误的方式解决这一切,但我想不出更好的方法。有任何想法吗?

4

1 回答 1

1

Ok, pointers :)

  • instead of using fast-enumeration, use a plain for-loop (for (int i=0; i < COUNT; i++))
  • store all indexes in a NSMutableIndexSet
  • use removeObjectsAtIndexes: to clear out all objects, by calling it on both arrays

edit: Or, alternatively, reverse the order of the for-loop ( int=COUNT-1; i >=0; i-- ) and remove the objects from the arrays directly.

于 2013-03-05T22:51:21.810 回答