Reputation: 1891
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
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