when does -tableView:cellForRowAtIndexPath: get ca

2020-08-01 11:12发布

问题:

I am having this issue with loading up table content in UITableViewController. I have created UITableViewController without the xib. In loadView I am calling a function which generates the array of data to be displayed. And from that array in tableView:cellForRowAtIndexPath: I am trying to load the cells. But somehow I am getting nothing on table. Besides that when I set break point for about call, it seems not get called. Can anyone tell what am I doing wrong here?

回答1:

-tableView:cellForRowAtIndexPath: gets called whenever new cell is needed, whether when first filling the screen with cells, or when a new cell is scrolled into the screen, but it will never get called if you haven't returned a proper number of sections and rows for your table view, as they will be 0 by default.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [yourArray count];
}


回答2:

cellforrowatindexpath is called when building the table. Have you set the delegate of the table to self? Have you added to your .h?



回答3:

as eimantas said, you have to set the "dataSource" delegate of your UITableView = (pointing to) yourClass with the tableView:cellForRowAtIndexPath method...

it's the method called whenever a new cell is needed (the first time you see your table it creates a needed number of cells to cover the whole height of your table height), then it's called when user scroll the table (not to create new cells, but to reuse the old cells with the new data needed)