0

I just want to add and delete rows in a table view very elegantly. So I asked this question. But I don't think the answers answer my question.

Then I thought that I can create an array of UITableViewCells and use that as the data source!

So I did something like this:

class MyTableViewController: UITableViewController {
    var cells = [[UITableViewCell]]()

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return cells.count
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cells[section].count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        return cells[indexPath.section][indexPath.row]
    }

    func addCellToSection(section: Int, cell: UITableViewCell) {
        cells[section].append(cell)
        tableView.reloadData()
    }

    func removeCellFromSection(section: Int, index: Int) {
        cells[section].removeAtIndex(index)
        tableView.reloadData()
    }
}

Now I can just call removeCellFromSection and addCellToSection to add and remove cells. How elegant!

However, I'm not sure whether calling tableView.reloadData() all the time will slow down the app. Each time I add or remove a cell, every cell in the table view needs to be refreshed. That would be a lot of work, right?

So will calling tableView.reloadData() all the time slow down my app significantly? If yes, what can be an alternative? If not, why not?

Additional information:

Initially, the table view contains 4 cells. The user can add cells and remove cells by pressing buttons. On average, the user would add less than 50 cells.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • 2
    Yes, it would be slow. – jtbandes May 18 '16 at 05:20
  • 2
    Sure,It would be slow,and also you cant expect the smooth tableview scrolling,If you reload data multiple times. – Shangari C May 18 '16 at 05:27
  • @ShangariC So are there any alternatives? – Sweeper May 18 '16 at 05:29
  • On reloading the tableview, currently presented cells will be recreated using `cellForRowAtIndexPath`, so whatever you implement in this method will add to it's performance.... Either way if you don't do this... presented cells will show old data but will be updated once they are out of the view and back again – sargeras May 18 '16 at 05:33
  • @sargeras I just access an array in `cellForRowAtIndexPath`! As I know, accessing an array is O(1) complexity so it can't be too slow, right? – Sweeper May 18 '16 at 05:35
  • @Sweeper I faced this issue,But I made a mistake in my logic part.In your case while adding and removing cell,you just refresh the particular cell not whole tableview entire cell – Shangari C May 18 '16 at 05:35
  • So, you have the array of `UITableViewCell`s ? – sargeras May 18 '16 at 05:36
  • @Sweeper,refer this link http://stackoverflow.com/questions/4448321/is-it-possible-to-refresh-a-single-uitableviewcell-in-a-uitableview and http://stackoverflow.com/questions/14702083/how-to-update-a-uitableviewcell-without-reloading-full-table and http://stackoverflow.com/questions/5418655/how-to-reload-and-animate-just-one-uitableview-cell-row – Shangari C May 18 '16 at 05:38
  • 1
    So, if you have similar cells you are implementing it wrong, that's why we use `reuseIdentifier` to update the cells with required values rather than creating or keeping lot of cells... keeping the cells in an array still keeps them in memory... rather hold the required data in an array to distinguish between cells and update them in cells that can be reused in `cellForRowAtIndexPath` – sargeras May 18 '16 at 05:40
  • 1
    Don't create array of cells. Create array of data to be used in cell and use it as a datasource. Take care of cell customization in `cellForRowAtIndexPath` and TableView will take care of displaying, hiding, scrolling, refreshing the cells. TableView reuses cells so your memory usage will be very reasonable. You don't have to do it yourself. And do look up a good tutorial on TableViews. – NSNoob May 18 '16 at 06:18
  • @NSNoob But my cells contains a bunch of `UITextField`s which I don't think is "data". How can I create a data source for a bunch of text fields? That's why I store a bunch of cells in an array. Because they are kind of like my "data". – Sweeper May 18 '16 at 10:18
  • @Sweeper You can add different kinds of cells in one tableView and then use those cells by modifying `cellForRowAtIndexPath` and assigning unique reuse identifiers to those cells. If that's all you are doing with cells, you most definitely don't need an array of cells. (Can't imagine any situation in which that would be warranted) – NSNoob May 18 '16 at 10:21

3 Answers3

3

If you have the indexPath of your cell

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathOfYourCell, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates]; 

It might help you.

Shangari C
  • 792
  • 1
  • 5
  • 17
2

Yes reloadData will slower its performance. As per Apple's Documentation when reload method is called all the data that is used to construct the table, including cells, section headers and footers, index arrays, and so on is updated and reloaded. So we should call this method when such a need arises like data or view is updated. For efficiency, the table view redisplays only those rows that are visible. It adjusts offsets if the table shrinks as a result of the reload. The table view’s delegate or data source calls this method when it wants the table view to completely reload its data. It should not be called in the methods that insert or delete rows, especially within an animation block implemented with calls to beginUpdates and endUpdates.

luckyShubhra
  • 2,731
  • 1
  • 12
  • 19
0

Yes it will be slow. Additionally if you store the UITableViewCells yourself it will use more memory than necessary because you'll be storing both your data and the cell instances. The UITableView programming system is designed to to a whole lot of work for you, but it does have a learning curve.

Additionally the mechanisms for adding and deleting table rows is elegant, but, like I said, does have a learning curve.

Apple provides Table View Programming Guide for iOS.

user212514
  • 3,110
  • 1
  • 15
  • 11