点cell会移动到tableview的第一行

参考:
iOS_UITableView 编辑(cell的插入, 删除, 移动)

获取tableviewCell在当前屏幕中的坐标值

UITableView__cell 距tableview顶端有间距

UITableView的contentsize设置//原理篇,比较有用

让UITableView自动滑动(定位)到某一行cell

一,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_selectIndexPath != indexPath){
  
        //2.移动cell
        CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];
        //这一步是取到当前的cell相对于整个屏幕的rect
        CGRect rect = [tableView convertRect:rectInTableView toView:[tableView superview]];
        //这一步是取到当前cell距离第一行的距离
        CGFloat contentOffsetY = rect.origin.y - tableView.frame.origin.y;
     
        [UIView animateWithDuration:0.3 animations:^(void) {
          //这一步其实也好理解了,看的到的要滑动的距离加上已经偏移的距离,等于总的要滑动的距离  
         [tableView setContentOffset:CGPointMake(0, contentOffsetY + tableView.contentOffset.y)];
            //此处可做UIView渐变效果以及飞出效果
        } completion:^(BOOL finished) {
            
        }];
        
        //记录选择的index
        _selectIndexPath = indexPath;
        
    }else{
       //第二次点击就跳到下一个controller
        [self jumpToShopDetail];
    }
}

二,


NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:10 inSection:0];

[[self tableView] scrollToRowAtIndexPath:scrollIndexPath
        atScrollPosition:UITableViewScrollPositionTop animated:YES];

第一种的好处是如果有上拉加载更多的话,结合一起用,效果貌似好点

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容