温馨提示×

温馨提示×

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

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

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

发布时间:2021-08-04 11:20:31 阅读:142 作者:小新 栏目:移动开发
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章给大家分享的是有关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 MyViewControllerUITableViewDataSource {
 
 // section数
 func numberOfSections(inUITableView) -> Int {
 }
 
 // row数
 public func tableView(_ tableViewUITableViewnumberOfRowsInSection sectionInt) -> Int {
 }
 
 // 在section和row下,cell
 public func tableView(_ tableViewUITableViewcellForRowAt indexPathIndexPath) -> 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 MyViewControllerUITableViewDelegate {
 func tableView(_ tableViewUITableViewviewForHeaderInSection sectionInt) -> UIView? {
 
 }
 
 func tableView(_ tableViewUITableViewviewForFooterInSection sectionInt) -> UIView? {
 
 }
}

高度

extension TTEntranceExamReportViewControllerUITableViewDelegate {
 func tableView(_ tableViewUITableViewheightForHeaderInSection sectionInt) -> CGFloat {
 }
 
 func tableView(_ tableViewUITableViewheightForFooterInSection sectionInt) -> CGFloat {
 }
}

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

func tableView(_ tableViewUITableViewdidSelectRowAt indexPathIndexPath) {
 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(frameCGRect(x0, y0, width1, height: CGFloat.leastNormalMagnitude))

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

需要强制实现下估算高度

传送门

func tableView(_ tableViewUITableViewestimatedHeightForRowAt indexPathIndexPath) -> CGFloat {
 return self.tableView(tableView, heightForRowAt: indexPath)
}

感谢各位的阅读!关于“iOS中使用UITableView常见问题有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

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

AI

开发者交流群×