温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS中使用UITableView常见问题有哪些

发布时间:2021-08-04 11:20:31 来源:亿速云 阅读:127 作者:小新 栏目:移动开发

这篇文章给大家分享的是有关iOS中使用UITableView常见问题有哪些的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

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中使用UITableView常见问题有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI