The right way to add data to a UITableView, quick

I am trying to add new data to a UITableView in two ways.

  • First way

    func insertData(appendMessages:[Message]) { var currentCount = self.messeges.count; var indxesPath:[NSIndexPath] = [NSIndexPath]() for msg in appendMessages { indxesPath.append(NSIndexPath(forRow:currentCount,inSection:0)); self.messeges.append(msg) currentCount++ } self.tableView.beginUpdates() self.tableView.insertRowsAtIndexPaths(indxesPath, withRowAnimation: UITableViewRowAnimation.Bottom) self.tableView.endUpdates() } 
  • Second way

     func insertData(appendMessages:[Message]) { for msg in appendMessages { self.messeges.append(msg) } self.tableView.reloadData() } 

You see that I shared the results.

When using "reloadData" everything works fine, but I think this is not a good reason, am I updating everything, and not new content?

When using "insertRowsAtIndexPaths", I have a duplicated separator, and the line is colored only when I click on it.

It's weird what I'm doing wrong ...

thanks

Shay

image where using reloadDataimage when using insertRowsAtIndexPaths

+6
source share
1 answer

The error is that you should initialize the counter to 0, and then increment it:

 func insertData(appendMessages:[Message]) { var currentCount = 0 var indexesPath = [NSIndexPath]() for _ in appendMessages { let index = let index = NSIndexPath(forRow: i, inSection: 0) indexesPath.append(index) currentCount++ } self.tableView.beginUpdates() self.tableView.insertRowsAtIndexPaths(indexesPath, withRowAnimation: UITableViewRowAnimation.Bottom) self.tableView.endUpdates() } 
-1
source

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


All Articles