cell点击后自动处于手机屏幕中心位置

云之声-语音评测项目

此处只是给出点击cell,当前的tableview会自动滑动到屏幕的中心。具体的数据根据自己的项目需求更改即可。


//获取当前cell在tableview中的位置

CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];

// tableview 总高度

NSInteger totleHeight = tableView.contentSize.height;

//选中cell 中心高度

NSInteger cellMid = rectInTableView.origin.y + rectInTableView.size.height/2.0;

//如果选中cell展开之后中心高度高于显示屏幕高一半就使其居中

if (cellMid > (KHeight - 64  - 49)/2 && totleHeight - cellMid > (KHeight - 64 - 49)/2) {

NSInteger offsetY = cellMid - (KHeight - 64 - 49)/2;

[tableView setContentOffset:CGPointMake(0, offsetY) animated:YES];

}

//如果选中cell展开之后中心高度与tableView总高度只差小于显示屏幕高一半,让tableView偏移量固定为tableView的高度与屏幕高之差

if (totleHeight - cellMid <= (KHeight - 64 - 49)/2) {

NSInteger offsetY = totleHeight - KHeight + 64 + 49 + 100;

[tableView setContentOffset:CGPointMake(0, offsetY) animated:YES];

}

//如果选中cell展开之后中心高度低于显示屏幕高一半,让tableView偏移量为-64

if (cellMid <= (KHeight - 64 - 49)/2) {

[tableView setContentOffset:CGPointMake(0, 0) animated:YES];

}

//如果 cell小于8条不移动

if (_spokenListCount<8) {

[tableView setContentOffset:CGPointMake(0, 0) animated:YES];

}



该功能核心代码已写出!此代码写在了didSelectRowAtIndexPath方法中。为了方便理解,特此在粘出截图:


代码截图

其它可能需要:

1、获取当前cell在tableview中的位置

CGRect rectintableview=[table rectForRowAtIndexPath:indexpath];

rectintableview.orygin.y

2、获取当前cell在屏幕中的位置

CGRect rectinsuperview = [table convertRect:rectintableview fromView:[table superview]];

rectinsuperview.orygin.y

3、tableview的contentOffset

table.contentOffset.y

4、tableview的总高度

tableView.contentSize.height


最后发现一个一步到位的方法:

只需一句话:

[self.allPrimaryPlayTV scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];




谢感谢大家的支持!有任何需要可以联系我!



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容