Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView reloadData duplicates data

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 clear my tableView before I reload?

like image 784
Slee Avatar asked Mar 13 '26 08:03

Slee


1 Answers

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.

like image 152
TechZen Avatar answered Mar 16 '26 07:03

TechZen