when does -tableView:cellForRowAtIndexPath: get called?

末鹿安然 提交于 2019-12-10 00:58:25

问题


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)



来源:https://stackoverflow.com/questions/9243238/when-does-tableviewcellforrowatindexpath-get-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!