滑动UITableViewCell [英] Slide UITableViewCell

查看:107
本文介绍了滑动UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是让一个 UITableViewCell 滑出屏幕的一侧(如在Twitter中),然后从另一侧滑回。我可以让单元格从屏幕向右滑动,但我似乎无法弄清楚如何让它从左边滑回到屏幕上。这是我的代码向右滑动它:

  UITableViewCell * cell = [self cellForRowAtIndexPath:indexPath]; 
[cell.layer setAnchorPoint:CGPointMake(0,0.5)];
[cell.layer setPosition:CGPointMake(cell.frame.size.width,cell.layer.position.y)];
CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@position.x];
[animation setRemovedOnCompletion:NO];
[animation setDelegate:self];
[animation setDuration:0.14];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[cell.layer addAnimation:animation forKey:@reveal];非常感谢您的帮助!

解决方案

UITableView 提供了插入和删除动画行的方法,并处理所有的动画。尝试这样:

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

然后在新单元格中滑动:

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

beginUpdates / endUpdates 方法会导致动画



注意两件事:


  1. 有很多表数据,插入可能需要很长时间(特别是如果你基本上替换整个表)。在这种情况下调用 [tableView reloadData] 更好,但是 deleteRowsAtIndexPath:withRowAnimation:视觉效果。


  2. 您必须确保支持表的实际数据相应地更改并正确。也就是说,如果要从表中删除或插入行,则提供表的数组(或任何数据)也必须正确反映表中每行的行数。



My goal is to have a UITableViewCell slide off one side of the screen (like in Twitter) and then slide back on from the other side. I'm able to make the cell slide off the screen to the right, but I can't seem to figure out how to get it to slide back onto the screen from the left right after. Here's my code to slide it off to the right:

UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath];
[cell.layer setAnchorPoint:CGPointMake(0, 0.5)];
[cell.layer setPosition:CGPointMake(cell.frame.size.width, cell.layer.position.y)];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.x"];
[animation setRemovedOnCompletion:NO];
[animation setDelegate:self];
[animation setDuration:0.14];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[cell.layer addAnimation:animation forKey:@"reveal"];

Thanks for any help!

解决方案

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.

这篇关于滑动UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆