开发者

UITableView reloadData duplicates data

开发者 https://www.devze.com 2023-01-06 05:18 出处:网络
I have 3 UITableViews on a UIView in which I load 3 different sets of data from NSMutableArrays.If I call [myTableView reloadData] the data kind of reloads - it actually gets added to the table along

I have 3 UITableViews on a UIView in which I load 3 different sets of data from NSMutableArrays. If I call [myTableView reloadData] the data kind of reloads - it actually gets added to the table along with what ever is already there. So 3 calls to reloadData results in triple listed information.

How do I prevent this from happening or how do I cle开发者_Go百科ar my tableView before I reload?


The most common cause of this is not reusing tableview cells. If you create a new cell for each logical row in the array (instead of dequeuing and reusing) the tableview may keep displaying all the cells. (**Edit:**probably won't happen.)

If not that, then it will be in one of the three methods that reloadData calls:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

Tableviews simply display the data. If the table duplicates then you're most likely duplicating the data in code somewhere. You want to make sure that one of these does not also cause the original source of the array to be read again and appended to the MutableArray that provides the data to the table.


Make use of the hasUncommittedUpdates method.

For instance:

if (self.tableview.hasUncommittedUpdates) { self.tableview.reloadData() } 

This clears the previous data and fetches the new one. Hope this helps

0

精彩评论

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