美文网首页
TableView 相关

TableView 相关

作者: 竹菜板 | 来源:发表于2017-03-25 12:00 被阅读16次
  • Group样式下第一个section的高度必须为1的问题解决:
override func viewDidLoad() {
    tableView.contentInset = UIEdgeInsets(top: -1, left: 0, bottom: 0, right: 0)
}
    
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if section == 0 {
        return 0.1
    } else {
        return 0
    }
}

  • 去掉空白行的横线
override func viewDidLoad() {
    tableView.tableFooterView = UIView()
}

  • TableView自动高度
override func viewDidLoad() {
    tableView.estimatedRowHeight = 80
    tableView.rowHeight = UITableViewAutomaticDimension
}

  • TableView自动高度不适用的情况,比如图片异步加载完毕后刷新Cell高度
var imgCellHeightList = [Int : CGFloat]() // 缓存高度的数组
cell?.imageview.setImage(url: url, holder: "logo", onCompletion: { [weak self] (image, err, _, _) in
    guard let sf = self else { return }
    if sf.imgCellHeightList[indexPath.row] != nil { return }
    if let img = image {
        let h = (img.size.height/img.size.width)*UIScreen.main.bounds.width
        sf.tableView.beginUpdates() // 关键代码
        sf.imgCellHeightList[indexPath.row] = h // 存储高度
        sf.tableView.endUpdates() // 关键代码
    } else {
        log(err?.localizedDescription)
    }
})

*取消 TableView plain模式下section header 悬停效果

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let offsetY = scrollView.contentOffset.y
    let sectionHeaderHeight: CGFloat = 49.0 // section高度
    if (offsetY <= sectionHeaderHeight && offsetY >= 0) {
        scrollView.contentInset = UIEdgeInsetsMake(-offsetY, 0, 0, 0)
    } else if (offsetY >= sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0)
    }
}

相关文章

网友评论

      本文标题:TableView 相关

      本文链接:https://www.haomeiwen.com/subject/cyqcottx.html