UITableView reloadData duplicates data

I have UITableViews on a UIView in which I load 3 different datasets from NSMutableArrays. If I call the [myTableView reloadData] reload data type, it is actually added to the table along with what ever already exists. Thus, 3 calls to reloadData result in the triple specified information.

How to prevent this, or how to clear the View table before reloading?

+3
source share
2 answers

The most common reason for this is not reusing table cells. If you create a new cell for each logical row in the array (instead of deleting and reusing it), the tableview can continue to display all the cells. (** Edit: ** probably won't happen.)

If it is not, then it will be in one of three methods that reloadDatacalls:

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

The tables simply display data. If the table is duplicated, you are likely to duplicate the data in the code somewhere. You want to make sure that one of them also does not cause the original source of the array to be read again and added to the MutableArray, which provides the data in the table.

+1
source

Use the method hasUncommittedUpdates.

For instance:

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

.

-1

Source: https://habr.com/ru/post/1753607/


All Articles