iOS新手学习

iOS --- 获取当前点击的cell 和刷新

2017-07-05  本文已影响816人  鑫飞

一: 获取当前点击cell

一般collectionView 或者 tableview都有自带的点击函数,如下:

1. collectionView
   -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//一般情况,cell不是自定义
    UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]; //即为要得到的cell
//自定义的cell
   TitleViewCell * cell = (TitleViewCell *)[self collectionView:collectionView cellForItemAtIndexPath:indexPath];//即为要得到的cell
}

2,tableView
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//非自定义cell
    UITableViewCell * cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
 //自定义cell
     NewsTableViewCell * cell = (NewsTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
}

二: 更新cell

1. 更新全部数据

UICollectionView 与 UITableView类似,都可以使用 reloadData 方法来进行所有 cell内容的更新,reloadData 过程没有默认的动画过程

2. 更新特定 cell 中的数据
UICollectionView
可以采用 reloadItemsAtIndexPaths方法
[self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
传入参数就是要刷新的 cell 所在的 indexPath组成的数组,但 reloadItemsAtIndexPath默认会有一个动画的过程,cell内容更新的瞬间会出现原内容与新内容重叠的情况,使用如下方式取消该动画即可 :
[UIView performWithoutAnimation:^{
    [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
}];
UITableView
UITableView 的 reloadSections方法也有同样的情况 :
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
上一篇下一篇

猜你喜欢

热点阅读