iOS中UITableView使用的常见问题总结

发布时间 - 2026-01-11 00:21:41    点击率:

1、如何设置headerView以及其高度

tableView.tableHeaderView = myHeaderView
 
let height = headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
var frame = headerView.frame
frame.size.height = height
headerView.frame = frame

2、去掉多余cell的分割线

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

3、如何设置section数、行数

extension MyViewController: UITableViewDataSource {
 
 // section数
 func numberOfSections(in: UITableView) -> Int {
 }
 
 // row数
 public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
 }
 
 // 在section和row下,cell
 public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 }
 
}

4、iOS 8+自动计算行高、section高度

tableView.estimatedRowHeight = 80
tableView.rowHeight = UITableViewAutomaticDimension

实际上,sectionHeader高度也可以自动算高

tv.estimatedSectionHeaderHeight = 20
tv.sectionHeaderHeight = UITableViewAutomaticDimension

当然sectionFooter也可以,不再赘述

5、禁用tableview自带的分割线

tv.separatorStyle = .none

6、设置sectionHeader和sectionFooter,以及他们的高度

view

extension MyViewController: UITableViewDelegate {
 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
 
 }
 
 func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
 
 }
}

高度

extension TTEntranceExamReportViewController: UITableViewDelegate {
 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
 }
 
 func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
 }
}

7、点击cell有阴影,抬起时候阴影消失

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 tableView.deselectRow(at: indexPath, animated: no)
 // other code
}

8、iPad的UITableViewCell自动缩进的问题

if (IS_IPAD && [_tableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)]) {
 _tableView.cellLayoutMarginsFollowReadableWidth = NO;
}

Swift版:

if IS_IPAD, #available(iOS 9.0, *) {
 tableView.cellLayoutMarginsFollowReadableWidth = false
}

9、设定UITableviewCell按下的点击效果

cell.selectedBackgroundView = [[PureColorView alloc] initWithColor:[UIColor redColor]];

PureColorView是将颜色转化为纯色View的类,网上可以搜到

10、sectionHeader不吸顶

let tv = UITableView(frame: CGRect.zero, style: .grouped)

11、使用.groupted后,TableView底部有20px多余空白

tv.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: CGFloat.leastNormalMagnitude))

12、ios 8系统上,点击cell push一个vc,再pop回来,部分cell高度会乱掉

需要强制实现下估算高度

传送门

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
 return self.tableView(tableView, heightForRowAt: indexPath)
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对各位iOS开发者们能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。


# ios  # uitableview优化  # uitableview  # 两列  # uitableview删除行  # ios UITableView实现无数据加载占位图片  # iOS中UIScrollView嵌套UITableView的实践教程  # IOS UITableView和NavigationBar的常用设置详解  # iOS基于UITableView实现多层展开与收起  # iOS中UITableView Cell实现自定义单选功能  # iOS中的UITableView的重用机制与加载优化详解  # IOS UITableViewCell详解及按钮点击事件处理实例  # IOS中UITableView滚动到指定位置  # IOS UITableView颜色设置的实例详解  # 如何设置  # 分割线  # 他们的  # 按下  # 这篇文章  # 自带  # 谢谢大家  # 转化为  # 可以自动  # 行数  # 传送门  # 网上  # 有疑问  # indexPath  # cellForRowAt  # public 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: 长沙做网站要多少钱,长沙国安网络怎么样?  Laravel怎么实现验证码(Captcha)功能  SQL查询语句优化的实用方法总结  作用域操作符会触发自动加载吗_php类自动加载机制与::调用【教程】  在线制作视频的网站有哪些,电脑如何制作视频短片?  php后缀怎么变mp4格式错误_修改扩展名提示格式不对怎么办【技巧】  Win11怎么恢复误删照片_Win11数据恢复工具使用【推荐】  laravel怎么使用数据库工厂(Factory)生成带有关联模型的数据_laravel Factory生成关联数据方法  mc皮肤壁纸制作器,苹果平板怎么设置自己想要的壁纸我的世界?  laravel怎么配置Redis作为缓存驱动_laravel Redis缓存配置教程  使用豆包 AI 辅助进行简单网页 HTML 结构设计  如何在IIS7上新建站点并设置安全权限?  HTML5空格和margin有啥区别_空格与外边距的使用场景【说明】  JavaScript实现Fly Bird小游戏  Python企业级消息系统教程_KafkaRabbitMQ高并发应用  如何快速搭建高效香港服务器网站?  惠州网站建设制作推广,惠州市华视达文化传媒有限公司怎么样?  高端智能建站公司优选:品牌定制与SEO优化一站式服务  Laravel如何使用Sanctum进行API认证?(SPA实战)  javascript中闭包概念与用法深入理解  Laravel怎么清理缓存_Laravel optimize clear命令详解  Python文件流缓冲机制_IO性能解析【教程】  如何挑选高效建站主机与优质域名?  如何利用DOS批处理实现定时关机操作详解  Android中AutoCompleteTextView自动提示  如何在Ubuntu系统下快速搭建WordPress个人网站?  jQuery中的100个技巧汇总  香港网站服务器数量如何影响SEO优化效果?  长沙企业网站制作哪家好,长沙水业集团官方网站?  Linux后台任务运行方法_nohup与&使用技巧【技巧】  Laravel如何操作JSON类型的数据库字段?(Eloquent示例)  夸克浏览器网页跳转延迟怎么办 夸克浏览器跳转优化  如何在建站之星网店版论坛获取技术支持?  Laravel如何生成API文档?(Swagger/OpenAPI教程)  EditPlus 正则表达式 实战(3)  如何用已有域名快速搭建网站?  JS碰撞运动实现方法详解  ,怎么在广州志愿者网站注册?  猎豹浏览器开发者工具怎么打开 猎豹浏览器F12调试工具使用【前端必备】  Laravel如何使用Contracts(契约)进行编程_Laravel契约接口与依赖反转  Bootstrap整体框架之CSS12栅格系统  Laravel怎么实现一对多关联查询_Laravel Eloquent模型关系定义与预加载【实战】  Laravel中Service Container是做什么的_Laravel服务容器与依赖注入核心概念解析  Laravel怎么配置.env环境变量_Laravel生产环境敏感数据保护与读取【方法】  Laravel用户密码怎么加密_Laravel Hash门面使用教程  WEB开发之注册页面验证码倒计时代码的实现  音响网站制作视频教程,隆霸音响官方网站?  Edge浏览器怎么启用睡眠标签页_节省电脑内存占用优化技巧  用v-html解决Vue.js渲染中html标签不被解析的问题  如何在服务器上三步完成建站并提升流量?