我是靠谱客的博主 调皮向日葵,这篇文章主要介绍iOS中UITableView使用的常见问题总结,现在分享给大家,希望可以做个参考。

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

复制代码
1
2
3
4
5
6
tableView.tableHeaderView = myHeaderView let height = headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height var frame = headerView.frame frame.size.height = height headerView.frame = frame

2、去掉多余cell的分割线

复制代码
1
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

3、如何设置section数、行数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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高度

复制代码
1
2
tableView.estimatedRowHeight = 80 tableView.rowHeight = UITableViewAutomaticDimension

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

复制代码
1
2
tv.estimatedSectionHeaderHeight = 20 tv.sectionHeaderHeight = UITableViewAutomaticDimension

当然sectionFooter也可以,不再赘述

5、禁用tableview自带的分割线

复制代码
1
tv.separatorStyle = .none

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

view

复制代码
1
2
3
4
5
6
7
8
9
extension MyViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { } func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { } }

高度

复制代码
1
2
3
4
5
6
7
extension TTEntranceExamReportViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { } func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { } }

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

复制代码
1
2
3
4
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: no) // other code }

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

复制代码
1
2
3
if (IS_IPAD && [_tableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)]) { _tableView.cellLayoutMarginsFollowReadableWidth = NO; }

Swift版:

复制代码
1
2
3
if IS_IPAD, #available(iOS 9.0, *) { tableView.cellLayoutMarginsFollowReadableWidth = false }

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

复制代码
1
cell.selectedBackgroundView = [[PureColorView alloc] initWithColor:[UIColor redColor]];

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

10、sectionHeader不吸顶

复制代码
1
let tv = UITableView(frame: CGRect.zero, style: .grouped)

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

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

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

需要强制实现下估算高度

传送门

复制代码
1
2
3
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { return self.tableView(tableView, heightForRowAt: indexPath) }

总结

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

最后

以上就是调皮向日葵最近收集整理的关于iOS中UITableView使用的常见问题总结的全部内容,更多相关iOS中UITableView使用内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(116)

评论列表共有 0 条评论

立即
投稿
返回
顶部