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里面拖进自己想要子视图或控件,再跟自定义类代码绑定。这样就更容易获取并控制了。

小弟拙见,欢迎指正补充,万分感谢。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容