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.

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

报告相同问题?