2017-09-21 45 views
0

我想在單個Tableview單元格中添加3個標籤& 3個圖像。 這裏是我的標籤&圖像視圖如何以編程方式在TableView單元格中添加多個UIImageView

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 
    }() 

這是我的設立觀點與約束

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])) 


    } 

的問題是這樣的工作只是罰款3個標籤&一個圖像不顯示功能其他兩個圖像

+0

*確切*問題是什麼? 「不顯示」那些其他兩個圖像有點模糊。例如,你有沒有設置一個斷點並確定'image2'和'image3'不是零?您是否收到任何控制檯反饋? – dfd

+0

是的,我已經做到了image2&image3不是零。 –

+0

這些元素的寬度是多少?似乎沒有設置。 – Larme

回答

1

我建議不要在代碼(*)中添加視圖到您的單元格。我建議設置你的表格視圖來使用自定義單元格模板。然後,您可以定義UITableViewCell的自定義一個或多個子類以及鏈接到圖像視圖的插口,並且當您使用您的特定標識符使一個單元出列時,您將獲得一個正確類的單元,並且可以將插座鏈接並準備設置。你所要做的就是將其轉換爲正確的類型,然後配置它的視圖。 (*)在代碼中向單元格添加視圖是更多的工作,並且還需要特殊處理。如果你不小心,那麼每次出隊時都會添加另一組視圖。當您修復該問題時,您需要一些機制來訪問您最初創建單元格時添加的視圖,並且像使用viewWithTag(_:)這樣的典型方法很脆弱。

+0

其實我正在編程的東西沒有任何事情與故事板:) –

+0

這是不明智的。 –

+0

你是對的,但這是我的任務這樣做:( –

相關問題