我正在寻找一种方法,使用来自两个独立(但并行)数组的图像和文本填充表格 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/