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)
}
}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.
- 让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
}
- 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
- 使用Nib注册Cell/Header/Footer时注意:
Nib只能有一个Top Level View,作为Cell/Header/Footer。不能添加任何其他顶级对象包括手势,否则将会崩溃。