0

我的 UITableView 有点问题。我正在使用从我们的服务器返回的一些 JSON 加载 tableView。

JSON 链接到 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 遍历天数组(部分)内容?

是否可以使用 UITableViews 循环遍历子数组?

任何帮助表示赞赏!谢谢,阿德里安

4

1 回答 1

2

这没有错误。cellForRowAtIndexPath对于当前可见的单元格,称为“按需”。当您向下滚动时,它将为剩余的单元格调用。

于 2013-09-04T10:58:26.520 回答