简体   繁体   中英

How to add multiple UIImageView in a TableView Cell programmatically

I am trying to add 3 labels & 3 images in a single Tableview cell. 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

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM