开发者

Should I clear table view before using reloadData?

开发者 https://www.devze.com 2023-02-21 15:00 出处:网络
I want to put new values in a table view. My tableView:开发者_运维问答cellForRowAtIndexPath: has the following

I want to put new values in a table view.

My tableView:开发者_运维问答cellForRowAtIndexPath: has the following

if (cell == nil) { // do something }

When I remove the if statement on top and use reloadData the app crashes. Do I need to clear the old values in the table before I use reloadData? If so how?

Thanks

Edit:

I'm sorry i didnt mean that i put reloadData inside tableView:cellForRowAtIndexPath:. i put it inside another method and removed if (cell == nil) { // do something } from inside tableView:cellForRowAtIndexPath:


You should read the TableView Programming Guide to familiarize yourself with the concepts. reloadData is a method that eventually calls tableView:cellForRowAtIndexPath:.


You are generating infinite loop by calling reloadData inside

tableView:cellForRowAtIndexPath:


There is no need to clear data in a table view. You may want to ensure you properly implement -prepareForReuse in your UITableViewCell subclasses though, so that reused cells don't show old data.

It's BTW unclear why you want to remove the if (cell == nil) bit. If there is no cell to reuse (dequeue) then you need to create a new one. If you don't you'll return nil, which will cause an exception in UITableView.


You (99% of the time) can't call reloadData from tableView:cellForRowAtIndexPath:. This is because reloadData eventually calls tableView:cellForRowAtIndexPath, so you get a never ending loop.

You need to explain more what you trying to do (and some extra code would be nice) in order to answer the question further.


reloadData calls all tableView data source , so you cann't call it in cellForRowAtIndexPath it will make an infinite loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消