我在包含属性(例如名称,图片和日期(日期只是picker view
中的字符串))的数组中有一个字典。
我想显示每周表格视图并按天排列项目。
我打算做的是每天创建一个新数组,将所有数据过滤到这些数组中,然后填充各个部分。有更聪明的方法吗?
如果我不先过滤数据,我想不出另一种获取numberOfRowsInSection
的方法。
最佳答案
另一种方法是每次需要返回-tableView:numberOfRowsInSection:
的值时,对您的词典数组进行实时过滤。你会
一些执行此操作的代码(未经编译,未经测试)可能类似于:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *day = [self _dayForTableViewSection:section]; // assuming this exists
NSPredicate *filter = [NSPredicate predicateWithBlock:^(id obj, NSDictionary *bindings) {
assert([obj isKindOfClass:[NSDictionary class]]); // array contains dictionaries
return [obj[@"day"] isEqualToString:day]; // assuming key is @"day"
}];
NSArray *matchingDictionaries = [self.allDictionaries filteredArrayUsingPredicate:filter]; // assuming data source is allDictionaries
return matchingDictionaries.count;
}
根据您的代码调用
-tableView:numberOfRowsInSection:
的频率以及完整数据源的大小,这可能会导致性能严重下降。您最好做一些您最初建议的工作:提前过滤数据并保持适当的数组为最新状态,以便在表视图中使用。 (尽管请记住,过早的优化通常弊大于利!)关于iphone - 用数据填充TableView节?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13948701/