Slide UITableViewCell

假装没事ソ 提交于 2019-12-04 13:57:09
MarkGranoff

UITableView provides methods to insert and remove rows with animation and handles all the animation for you. Try something like this:

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:myIndexPaths
                 withRowAnimation:UITableViewCellRowAnimationRight];
[tableView endUpdates];

And then to slide in a new cell:

[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:myNewIndexPaths
                 withRowAnimation:UITableViewCellRowAnimationLeft];
[tableView endUpdates];

The beginUpdates/endUpdates methods cause the animations to be batched up and executed all at once.

Beware of 2 things:

  1. With a lot of table data, the insert can take a long time (expecially if you are essentially replacing the whole table). Calling [tableView reloadData] works better in this case, but the deleteRowsAtIndexPath:withRowAnimation: can still be used for a nice visual effect.

  2. You have to be sure that the actual data backing your table changes accordingly and correctly. That is to say, if you are removing or inserting rows form the table, then your array (or whatever) that is feeding the table must also correctly reflect how many rows are in the table at each step.

The behavior you want should be done by animating subviews of the cell, not the cell itself. The cell should stay in place in the table. Create a custom cell with the subviews you want in the contentArea and then slide those on/off as you like.

The standard UIView block based animations should work great for this as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!