user3353890
user3353890

Reputation: 1891

Do I have to use both of these methods?

When I expand a UITableViewCell on touch, I know I have to update the UITableView. Right now I'm doing:

tableView.beginUpdates()
tableView.reloadRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.Automatic)
tableView.endUpdates()

Do I need to use both updates methods as well as the reload method? Or is it just one or the other? I'm not completely understanding, so a little explanation would be fantastic. Thanks!

Upvotes: 0

Views: 78

Answers (1)

Gandalf
Gandalf

Reputation: 2417

No, you don't have to use both. Either you go with reloadCell technique or cell updates via beginUpdate and endUpdate.

When you are reloading a particular row, internally table view system creates 2 cell and then blends in the new one with. You can remove the beginUpdates and endUpdates and simply call this to change the height. But animation won't be smooth and you will observe the line for small cell while animation back from the larger cell height.

As for beginUpdates and endUpdates they are used for group insertion, deletion etc. but it is an old hack to forcefully call the table view delegates and get the changed height for selected index. This technique handles the height change animation quite well. So your code will be like

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Write the code to track the selected index path.
    tableView.reloadRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.Automatic)
}

OR

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath          
 {
     [tableView beginUpdates]; // tell the table you're about to start making changes
     //Write the code to track the selected index path.
    [tableView endUpdates]; // tell the table you're done making your changes
 }

Upvotes: 3

Related Questions