简体   繁体   English

如何以编程方式在TableView单元格中添加多个UIImageView

[英]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.

相关问题 在TableView单元格中加载和缓存多个UIImageView不起作用 - Loading and caching multiple UIImageView in a tableview cell not working 以编程方式在UIScrollview中添加多个UIImageview - Add multiple UIImageview in UIScrollview programmatically 如何调整图像大小以适应表视图单元格中的 UIImageview - how to resize the image to fit in UIImageview in a tableview cell 如何在自定义tableview单元格下调整UIImageview - How to adjust the UIImageview under custom tableview cell 如何以编程方式将不同单元格的不同图像添加到tableView(Swift 3) - How to add different images for different cell to a tableView programmatically (Swift 3) 如何以编程方式将约束添加到tableView单元格的默认textLabel和annexView - How to Add constraints programmatically to default textLabel and accessoryView of tableView cell 如何以编程方式将不同单元格的不同图像添加到tableView(Swift) - How to add different images for different cell to a tableView programmatically (Swift) 如何将单元格添加到tableview中 - how to add to cell into tableview 在Swift中,如何在TableView中的TableViewCell中添加UIImageView上的徽章? - In Swift, How to add Badges on UIImageView in TableViewCell in TableView? 如何以编程方式在TableView上添加WKWebView - How add WKWebView on TableView programmatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM