代码基于:swift 4.0
- 在一个类中,可以通过 customClass.self 取出不带命名空间的类名。
print(self) // <testOne.ViewController: 0x1033094f0>
print(self.description) // <testOne.ViewController: 0x101d10350>
print(ViewController.self) // ViewController
po type(of: self) // testOne.ViewController
print(type(of: self)) // ViewController
- type(of:)得到是类型
let count: Int = 5
printInfo(count)
// '5' of type 'Int'
- 为一个tableViewCell设置一个唯一标志符,通常让cell 遵循一个协议,在协议里面定义一个uniqueIdentifier。用自己的类名作为唯一标识符。
// 1. 定义一个协议
protocol CellProtocol: class {
static var uniqueIdentifier: String {get}
}
// 2. 协议扩展来实现属性
extension CellProtocol {
static var uniqueIdentifier: String {
return String(describing: type(of: self)).components(separatedBy: ".").first!
}
}
// 3.调用
// 先遵守协议
class TPPReviewCell: UITableViewCell, CellProtocol {
}
// 在用到这个cell的cellForRow里面
let cell = tableView.dequeueReusableCell(withIdentifier: TPPReviewCell.uniqueIdentifier) as! TPPReviewCell
-
xib中cell的自动计算行高。xib中固定部分的空间,高度一定要是固定值(就算label可以根据字体计算出行号,为避免xib中红色错误提示,也给label设置固定值),通常只有红色Label部分是变化的高度,如果要所有文字全部显示,那么label的numberOfLines设置为0(我这个图设置的是最多只显示5行)
cell自动计算行高
// 1. 初始化tableView
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 116.0
//2. 设置tableView的行高时
if (某些行需要固定的高度) {
return 固定高度
} else {
return UITableViewAutomaticDimension
}
网友评论