实现了上下两个cell交换的简单动效,界面如下,点击上,和上面一个cell交换位置,点击下,和下面一个交换位置,点击删除,就删除当前的cell,完整代码,简单说明一下主要cell交换的代码。

UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
NSIndexPath *nextIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
UITableViewCell *nextCell = [_tableView cellForRowAtIndexPath:nextIndexPath];
CGFloat cellY = cell.frame.origin.y;
[UIView animateWithDuration:0.6 animations:^{
//当前这个顶部就和上面一个平齐
CGRect rect = cell.frame;
rect.origin.y = cellY + CGRectGetHeight(nextCell.frame);
cell.frame = rect;
//上面一个需要处理一下,只能基于当前的y+下面的cell的高度,直接交换y轴,位置不准,在刷新表格的时候,会有抖动的现象
CGRect rect1 = nextCell.frame;
rect1.origin.y = cellY;
nextCell.frame = rect1;
}completion:^(BOOL finished)
{[titleArray exchangeObjectAtIndex:index

这篇博客介绍了如何在UITableView中实现相邻cell的交换动画。点击cell时,它会与上方或下方cell进行交换,同时提供删除功能。关键代码涉及获取indexPath,计算cell的y坐标,并在动画完成后更新数据源和刷新表格。需要注意的是,当cell部分显示时,获取前一个cell的y坐标可能不准确,建议存储每个cell的y坐标以避免此类问题。
最低0.47元/天 解锁文章
742

被折叠的 条评论
为什么被折叠?



