beginUpdates和endUpdates 实现UITableView的动画块

我们在做UITableView的修改,删除,选择时,需要对UITableView进行一系列的动作操作。

(以下是删除动画效果)


这样,我们就会用到
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [_currentData removeObjectAtIndex:indexPath.row];
        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        [tableView endUpdates];
    }
} 

向上面一段代码,就是动态删除UITableView 的UITableViewCell的操作。
 
所以,就需要使用beginUpdates方法和endUpdates方法,将要做的删除操作“包”起来!

beginUpdates方法和endUpdates方法是什么呢?

这两个方法,是配合起来使用的,标记了一个tableView的动画块。
分别代表动画的开始开始和结束。
两者成对出现,可以嵌套使用。
一般,在添加,删除,选择 tableView中使用,并实现动画效果。
在动画块内,不建议使用reloadData方法,如果使用,会影响动画。
 
一般什么时候使用这么一个动画块呢?

一般在UITableView执行:删除行,插入行,删除分组,插入分组时,使用!用来协调UITableView的动画效果。

插入指定的行,
在执行该方法时,会对数据源进行访问(分组数据和行数据),并更新可见行。所以,在调用该方法前,应该先更新数据源
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

插入分组到制定位置
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
插入一个特定的分组。如果,指定的位置上已经存在了分组,那么原来的分组向后移动一个位置。

删除制定位置的分组
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
删除一个制定位置的分组,其后面的分组向前移动一个位置。

移动分组
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection
移动原来的分组从一个位置移动到一个新的位置。如果,新位置上若存在某个分组,那这某个分组将会向上(下)移动到临近一个位置。该方法,没有动画参数。会直接移动。并且一次只能移动一个分组。

在如上方法中,建议使用该动画块进行操作!
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UITableView循环滚动可以通过以下步骤实现: 1. 首先,创建一个UITableView并设置其数据源和代理。 2. 然后,创建一个数组来存储要显示的数据。 3. 实现UITableViewDataSource协议中的方法tableView(_:numberOfRowsInSection:),返回一个比实际数据量多的数值,例如10倍于实际数据量。 4. 实现tableView(_:cellForRowAt:)方法,在该方法中设置cell的内容,需要根据cell的indexPath.row来获取数据,应该将indexPath.row对实际数据量取模,以保证循环滚动。 5. 实现tableView(_:willDisplay:forRowAt:)方法,在该方法中判断当前cell是否为最后一行,如果是,则将tableView的contentOffset设置到第一行的位置,以实现循环滚动。 下面是一个简单的实现代码: ```swift class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var tableView: UITableView! var data = ["A", "B", "C", "D", "E"] override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = self tableView.delegate = self tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell") } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return data.count * 10 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) cell.textLabel?.text = data[indexPath.row % data.count] return cell } func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { let lastRowIndex = tableView.numberOfRows(inSection: 0) - 1 if indexPath.row == lastRowIndex { let firstIndexPath = IndexPath(row: 0, section: 0) tableView.scrollToRow(at: firstIndexPath, at: .top, animated: false) } } } ``` 在这个例子中,我们将数据循环滚动10次,即实际数据量为5个,但是显示了50个cell,可以通过手动滑动tableView来验证循环滚动效果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值