1.笨蛋型獲取
eg:在一個for循環里面,獲取子視圖label
for cell in tableView.visibleCells{
let label = cell.subViews.first?.subViews.first as! UILabel
if label.text!.isEmpty{
...
}
}
2.上進型獲取
eg:在一個for循環里面,獲取子視圖label
for cell in tableView.visibleCells{
let label = cell.contentView.subViews[0] as! UILabel
if label.text!.isEmpty{
...
}
}
3.高效型獲取
前提是給子視圖指定一個tag數。
eg:在一個for循環里面,獲取子視圖uitextfield
for cell in tableView.visibleCells{
if let label = cell.viewWithTag(2) {
let lab = label as! UITextField
print(lab.text)
...
}
}
4.局限型獲取
前提是cell類為原始的類,而且cell的style為非custom
cell.textLabel?.text = "Grandre"
cell.detailTextLabel?.text = "ChinaSwift"
//獲取cell的imageView,
cell.imageView?.image = UIImage(named: "Grandre")
cell.imageView?.layer.cornerRadius = 22
cell.imageView?.layer.masksToBounds = true
當然,如果自定義類的時候,cell里面拖進自己想要子視圖或控件,再跟自定義類代碼綁定。這樣就更容易獲取并控制了。
小弟拙見,歡迎指正補充,萬分感謝。