UITableView

  1. customize UITableViewCell's highlighted state
    正确:
    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
    super.setHighlighted(highlighted, animated: animated)
    //do your custom appearance
    }
    错误:
    override var isHighlighted: Bool {
    didSet {
    //won't be called before tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath)
    }
    }

  2. group style 的 UITableView 的第一个section 的 header
    自定义section header view:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?

时必须指定高度:

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat

否则,第一个section header 会没有高度而无法显示
同理,footer也是,否则最后一个section footer 的高度显示不正确。
引自苹果文档:

This method only works correctly when tableView(_:heightForHeaderInSection:) is also implemented.

  1. 让cell 或者 section 透明:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        if let header = view as? UITableViewHeaderFooterView {
            header.backgroundView?.backgroundColor = nil
        }
    }
    
func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
        if let footer = view as? UITableViewHeaderFooterView {
            footer.backgroundView?.backgroundColor = nil
        }
    }
    
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.backgroundColor = nil
    }

  1. rowHeight 和 estimatedRowHeight 用于自适应Cell时需要注意:
  • rowHeight
    Note that if you create a self-sizing cell in Interface Builder, the default row height is changed to the value set in Interface Builder. To get the expected self-sizing behavior for a cell that you create in Interface Builder, you must explicitly set rowHeight equal to UITableViewAutomaticDimension in your code. 代码中需要明确设置 rowHeight = UITableViewAutomaticDimension

  • estimatedRowHeight
    The default value is 0, which means there is no estimate.
    When you create a self-sizing table view cell, you need to set this property and use constraints to define the cell’s size. 即estimatedRowHeight不能为0

  1. 使用Nib注册Cell/Header/Footer时注意:
    Nib只能有一个Top Level View,作为Cell/Header/Footer。不能添加任何其他顶级对象包括手势,否则将会崩溃。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 版权声明:未经本人允许,禁止转载. 1. TableView初始化 1.UITableView有两种风格:UITa...
    萧雪痕阅读 7,911评论 2 10
  • #pragma mark someValueAboutTableView 1.tableView的样式:UITab...
    潇岩阅读 4,571评论 0 0
  • 最近做的项目中用的UITableView比较多,在这里对UITableView的使用简单做一下总结。 @proto...
    Kylin125阅读 3,998评论 0 49
  • 1.创建一个UITableView对象 ITableView *tableView = [[UITableView...
    桐生一猫阅读 5,275评论 0 5
  • 1 成韵和晓楠的相识就是源于那场饭局,成韵至今历历在目。 一年前的春末夏初,同事约了成韵的饭,比成韵大不少的遇姐很...
    729fca9ffb1f阅读 1,738评论 1 0

友情链接更多精彩内容