Swift_tableView_cell中子視圖的獲取方式

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里面拖進自己想要子視圖或控件,再跟自定義類代碼綁定。這樣就更容易獲取并控制了。

小弟拙見,歡迎指正補充,萬分感謝。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容