【发布时间】:2013-09-04 10:35:36
【问题描述】:
我的 UITableView 有点问题。 我正在使用从我们的服务器返回的一些 JSON 加载 tableView。
JSON Link to Pastebin >
(为了便于阅读,我删除了一些项目,因为它包含数百个项目) 这是 PHP 的 print_r() 输出。
如您所见,它使用子数组。 当我从 numberOfRowsInSection 记录部分和计数时,这是正确的。
例如,
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([tableView isEqual: self.timetableTableView]){
NSDictionary *sectionContents = [_timetableDataArray objectAtIndex: section];
NSLog(@"Section: %d, count: %d", section, [sectionContents count]);
return [sectionContents count];
}
return 0;
}
返回,
Section: 6, count: 12
Section: 0, count: 35
Section: 1, count: 35
Section: 2, count: 38
Section: 3, count: 33
Section: 4, count: 19
Section: 5, count: 15
到目前为止一切顺利。 表格视图还为每个部分绘制了正确数量的部分和单元格。
现在,我的问题在于 cellForRowAtIndexPath。 它没有被称为我期望的次数。我不确定我做错了什么。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([tableView isEqual: self.timetableTableView]){
static NSString *TimetableCellIdentifier = @"TimetableTableCell";
TimetableTableCell *cell = (TimetableTableCell *)[tableView dequeueReusableCellWithIdentifier:TimetableCellIdentifier];
if(cell == nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TimetableTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// Get the day array out of my JSON.
NSDictionary *dayContents = [_timetableDataArray objectAtIndex:indexPath.section];
NSLog(@"Row: %d, Path: %d", indexPath.row, indexPath.section);
return cell;
}
}
这个日志,
Row: 0, Path: 0
Row: 1, Path: 0
Row: 2, Path: 0
Row: 3, Path: 0
Row: 4, Path: 0
Row: 5, Path: 0
numberOfSectionsInTableView returns the correct amount of sections.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if([tableView isEqual: self.timetableTableView]){
NSLog(@"sections: %d", [_timetableDataArray count]);
return [_timetableDataArray count];
}
return 0;
}
sections: 7
据我所知, cellForRowAtIndexPath 仅在我的 JSON 中遍历 days 数组。 尽管它在 numberOfRowsInSection 中有正确数量的部分/单元格。
如何告诉 cellForRowAtIndexPath 遍历 days 数组(部分)的内容?
是否可以使用 UITableViews 循环遍历子数组?
感谢任何帮助! 谢谢, 阿德里安
【问题讨论】:
-
你实现了 numberOfSectionsInTableView 吗?
-
cellForRowAtIndexPath仅对 当前可见 单元格调用。如果在表格视图中向下滚动会发生什么? -
迈克,我已经用额外的代码更新了我的帖子。 numberOfSectionsInTableView 返回正确数量的部分。
-
@MartinR,啊,我明白了!我没有意识到它在您滚动时改变了行/路径。我觉得这就是。愚蠢的我!
标签: php ios objective-c json uitableview