[英]How to add multiple UIImageView in a TableView Cell programmatically
I am trying to add 3 labels & 3 images in a single Tableview cell. 我正在尝试在单个Tableview单元中添加3个标签和3个图像。 here's my labels & image views
这是我的标签和图片视图
let Namelbl:UILabel={
let label=UILabel()
label.text="item"
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let Namelbl2:UILabel={
let label2=UILabel()
label2.text="item"
label2.translatesAutoresizingMaskIntoConstraints = false
return label2
}()
let Namelbl3:UILabel={
let label3=UILabel()
label3.text="item"
label3.translatesAutoresizingMaskIntoConstraints = false
return label3
}()
let image1: UIImageView = {
let theImageView1 = UIImageView()
theImageView1.image = UIImage(named: "Masjid")
theImageView1.translatesAutoresizingMaskIntoConstraints = false
return theImageView1
}()
let image2: UIImageView = {
let theImageView2 = UIImageView()
theImageView2.image = UIImage(named: "Masjid2")
theImageView2.translatesAutoresizingMaskIntoConstraints = false
return theImageView2
}()
let image3: UIImageView = {
let theImageView3 = UIImageView()
theImageView3.image = UIImage(named: "Masjid3")
theImageView3.translatesAutoresizingMaskIntoConstraints = false
return theImageView3
}()
And here's my function for setting up view with constraints 这是我设置约束视图的功能
func setupViews(){
addSubview(Namelbl)
addSubview(Namelbl2)
addSubview(Namelbl3)
addSubview(image1)
addSubview(image2)
addSubview(image3)
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-16-[v0]-16-[v1]-16-[v2]-16-[v3]-16-[v4]-16-[v5]-50-|",options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": Namelbl,"v1":Namelbl2,"v2":Namelbl3,"v3":image1,"v4":image2,"v5":image3]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": Namelbl]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": Namelbl2]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": Namelbl3]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": image1]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": image2]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": image3]))
}
the problem is this works just fine for 3 labels & one image it doesn't show the other two images 问题是,这对于3个标签和一张图片来说效果很好,而一张图片却不显示其他两张图片
I would advise against adding views to your cells in code(*). 我建议不要在代码(*)中向您的单元格添加视图。 I suggest setting up your table view to use custom cell templates.
我建议设置表格视图以使用自定义单元格模板。 You can then define custom one or more subclasses of UITableViewCell with outlets that link to your image views, and when you dequeue a cell with your specific identifier you'll get a cell of the correct class with outlets linked and ready to set up.
然后,您可以定义具有链接到图像视图的出口的UITableViewCell的自定义一个或多个子类,并且当使用特定标识符对单元格进行出队时,您将获得具有链接的出口的正确类的单元格并可以进行设置。 All you have to do is cast it to the correct type and then configure it's views.
您要做的就是将其转换为正确的类型,然后配置其视图。
(*) Adding views to cells in code is more work, and it also requires special handling. (*)将视图添加到代码中的单元格需要更多工作,并且还需要进行特殊处理。 If you're not careful then every time you dequeue a cell you add another set of views.
如果您不小心,那么每次您将一个单元出队时,都会添加另一组视图。 When you fix that you need some mechanism to access the views you added when the cell was initially created, and a typical approach like using
viewWithTag(_:)
is fragile. 修复该问题后,您需要某种机制来访问最初创建该单元格时添加的视图,并且使用
viewWithTag(_:)
类的典型方法非常脆弱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.