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.

Eg,

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

Returns,

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 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形