2012-12-19 64 views
2

我有一个数组中的字典,其中包含诸如名称,图片和日期等属性(日期只是来自picker view的字符串)。用数据填充TableView部分?

我想显示每周表格视图并按日排列项目。

我打算做的是每天创建一个新的数组,将所有数据过滤到这些数组中,然后填充这些部分。有没有更聪明的方法呢?

如果我不先过滤数据,我无法想到另一种方式获得numberOfRowsInSection

回答

1

另一种方法是在每次需要返回-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:和您的完整的数据源的大小,这库仑d招致相当严重的表现。您可能会更好地执行您最初的建议:提前过滤数据,并将适当的阵列保持最新,以便在您的表格视图中使用。 (虽然要记住,过早的优化往往会造成更多的伤害,而不是好的!)

+0

我已经写了我上面提出的建议,但仍然无法理解。我有7个数据阵列。我怎样才能使用'(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath'因此每个数组将在一个节? – Segev

+0

这几乎听起来像是一个单独的问题:)但你要做的是使用'indexPath.section'确定使用哪个数组(使用7个),然后使用'indexPath.row'作为索引该数组。使用您提取的任何对象来填充您的出队UITableViewCell并将其返回。 – Tim