objective-c - 如何从两个数组填充 TableView 内容?

标签 objective-c c uitableview nsmutablearray nsarray

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

(profileList 从中获取初始值的 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,但不适用于buttonList。 (此后,在 tableView:cellForRowAtIndexPath: 中,cell.textLabel.text 设置正常,但 cell.imageView.image 抛出异常。)

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

看来我的处理方式完全错误,但我无法想出更好的方法。有什么想法吗?

最佳答案

好的,请指点:)

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

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

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

相关文章:

ios - UITextView 和 UILabel subview 属性为零

objective-c - 开发版本和 adhoc/release 之间有任何#define 或类似的区别吗?

c - int 80 不出现在汇编代码中

c - 动态数据结构

ios - 从 TableView 中删除时索引超出范围?

swift - 带有 UISearchControllerDelegate 和/或 UISearchBarDelegate 和/或 UISearchController 的搜索栏

iphone - 如何截取 View 的屏幕截图并通过电子邮件发送?

c - 确保一个 cpu 在另一个 cpu 读取 "double"之前写入了 "double"?

iOS Table View Cell Swift 4 中的滚动视差效果

ios - 如何在 Objective-C 中将一个 NSMutableArray 存储到另一个 NSMutableArray