源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-08-24 20:21 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:iOS中UITableView使用的常见问题总结
[b]1、如何设置headerView以及其高度[/b]
tableView.tableHeaderView = myHeaderView
 
let height = headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
var frame = headerView.frame
frame.size.height = height
headerView.frame = frame
[b]2、去掉多余cell的分割线[/b]
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
[b]3、如何设置section数、行数[/b]
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 {
 }
 
}
[b]4、iOS 8+自动计算行高、section高度[/b]
tableView.estimatedRowHeight = 80
tableView.rowHeight = UITableViewAutomaticDimension
实际上,sectionHeader高度也可以自动算高
tv.estimatedSectionHeaderHeight = 20
tv.sectionHeaderHeight = UITableViewAutomaticDimension
当然sectionFooter也可以,不再赘述 [b]5、禁用tableview自带的分割线[/b]
tv.separatorStyle = .none
[b]6、设置sectionHeader和sectionFooter,以及他们的高度[/b] 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 {
 }
}
[b]7、点击cell有阴影,抬起时候阴影消失[/b]
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 tableView.deselectRow(at: indexPath, animated: no)
 // other code
}
[b]8、iPad的UITableViewCell自动缩进的问题[/b]
if (IS_IPAD && [_tableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)]) {
 _tableView.cellLayoutMarginsFollowReadableWidth = NO;
}
Swift版:
if IS_IPAD, #available(iOS 9.0, *) {
 tableView.cellLayoutMarginsFollowReadableWidth = false
}
[b]9、设定UITableviewCell按下的点击效果[/b]
cell.selectedBackgroundView = [[PureColorView alloc] initWithColor:[UIColor redColor]];
PureColorView是将颜色转化为纯色View的类,网上可以搜到 [b]10、sectionHeader不吸顶[/b]
let tv = UITableView(frame: CGRect.zero, style: .grouped)
[b]11、使用.groupted后,TableView底部有20px多余空白[/b]
tv.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: CGFloat.leastNormalMagnitude))
[b]12、ios 8系统上,点击cell push一个vc,再pop回来,部分cell高度会乱掉[/b] 需要强制实现下估算高度 [url=http://stackoverflow.com/questions/25221031/uitableview-layout-messing-up-on-push-segue-and-return-ios-8-xcode-beta-5-sw]传送门[/url]
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
 return self.tableView(tableView, heightForRowAt: indexPath)
}
[b]总结[/b] 以上就是这篇文章的全部内容了,希望本文的内容对各位iOS开发者们能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对编程素材网的支持。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部