简体   繁体   中英

iOS: cellForRowAtIndexPath not being called immediately after numberOfRowsInSection

I have my uitable data being populated for the current section inside numberOfRowsInSection . I then expect it to call cellForRowAtIndexPath so that I can use that data.

In my numberOfRowsInSection I am overwriting the data from the previous section. Doing NSLog it shows that numberOfRowsInSection runs for all 149 sections before the first cellForRowAtIndexPath is called... Why?

I have removed calls to reloadData since I saw it was the issue for someone else here on stack overflow but that didn't resolve the issue.

Per Jim I am adding what I am doing.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{   
    NSMutableArray *tempArray = [NSMutableArray array];
    FMResultSet *appointmentResults = [[DataClass getDB] executeQuery:@"SELECT * FROM Appointments WHERE date = ?",[self.appointmentDates objectAtIndex:section]];
    int rowCount = 0;
    while ([appointmentResults next]) {
        [tempArray addObject:[appointmentResults resultDict]];
        rowCount++;
    }
    self.appointments = tempArray;
    NSLog(@"Appointments: %@ - %i", [self.appointmentDates objectAtIndex:section], rowCount);
    return rowCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Parse the appointments for the current section and display them
}

In a general sense, it's not very good practice to rely upon Apple calling your delegate functions in any particular pattern.

With UITableView in particular, it only will request data on a need-to-know basis. The UITableView has a greater need to know how many sections and rows there are in the entire table so that it can handle things like scrolling position and scrollbar size properly, while actual cell contents can be allowed to slide until they're about to enter the user's view.

You can't take a call to numberOfRowsInSection as implying anything more than that the system wants to know how many rows are in a section. :)

Its the behavior of UITableView.

It will load only those cells which are visible. To calculate it, it will ask number of rows for each sections before displaying them.

The cells which are not visible, cellForRowAtIndexPath will not get called initially. It will get called once user scrolls through it.

I also get the same situation. i think the reason is that the heightof row return by the table is 0.

我的猜测是uitableview会缓存数据,因此运行一次并获取所有内容

It is how the UITableView class works..

Nothing to be shocked at..and you won't be able to change that..

Also 149 sections should not be loaded at once.. load them as the user scrolls..

If you want to refresh/change your table you should change appropriate data in model , then call one of tableView's [tableView reload....]; methods (depends on what exactly you're trying to change).

UITableViewDelegate methods (especially those returning number of rows, sections...) shouldn't do much more than their name says or you'll end up with a total mess of your table or possibly app crashing...

When you call [tableView reload....]; your content-providing methods will get called and that's the place to provide, well, content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM