duanliang4009 2013-09-04 10:35
浏览 55

iOS - UITableView - numRowsInSection与cellForRowAtIndexPath不同

I'm having a little trouble with my UITableView. I'm loading the tableView with some JSON returned from our server.

JSON Link to Pastebin >

(I've removed some items for readability as it contained hundreds) This is the print_r() output from PHP.

As you can see it's using sub arrays. When I log sections and counts from numberOfRowsInSection, this is correct.


- (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

So far so good. The table view also draws the correct amount of sections and cells, per section.

Now, my problem is in cellForRowAtIndexPath. It's not being called the amount of times i'd expect. I'm not sure what i'm doing wrong.

- (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;

This logs,

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

As far as I can tell, cellForRowAtIndexPath is iterating over the days array in my JSON only . Although it has the correct amount of sections/cells in numberOfRowsInSection.

How can I tell cellForRowAtIndexPath to iterate over the days array (section) contents?

Is it possible to loop through sub arrays with UITableViews?

Any help is appreciated! Thanks, Adrian

  • 写回答

1条回答 默认 最新

  • dqq9695 2013-09-04 10:58

    This is no error. cellForRowAtIndexPath is called "on demand" for the currently visible cells. When you scroll down, it will be called for the remaining cells.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 wamp3.3.5安装完成后图标正常显示绿色,鼠标左右键点击图标均无反应。求解决方法。
  • ¥15 鼠标点击的这条记录了什么?
  • ¥15 在写pid调速的程序时,电机始终维持最大速度
  • ¥15 帅锅们,我很想知道这个论坛是用什么模板搭的(T_T)
  • ¥15 请问如何查看手机root记录?
  • ¥15 商城小程序订单号重复
  • ¥15 学校优化算法sbo和蚁群算法怎么结合
  • ¥21 matlab怎么求时域信号的二阶导数
  • ¥15 判断两个表是否完全相同
  • ¥15 java map类型数据格式,如何快速通过前缀匹配元素