2017-05-31 44 views
0

我有三個imagesviews在我看來,細胞,他們目前都顯示相同的圖像我想知道如何使每個圖像的單獨圖像,可能像 顯示兩個不同的圖像如果香蕉其他的圖像= imagebanana顯示圖像,如果蘋果的形象= imageapple顯示圖像,如目前在每個單獨的單元中的所有圖像香蕉,香蕉,香蕉,我想有香蕉,蘋果,梨在一個小區?迅速 - 如何在定製視圖細胞

struct cellData { 

let cell: Int! 
let text: String! 
let image: UIImage! 
} 



class HomepageVC2JAREDTutorial: UITableViewController { 


var arrayofCellData = [cellData]() 



override func viewDidLoad() { 
    super.viewDidLoad() 

    arrayofCellData = [cellData(cell : 1, text : "tom",image : #imageLiteral(resourceName: "tom3")),cellData(cell : 2, text : "denis",image : #imageLiteral(resourceName: "denis2")),cellData(cell : 1, text : "mark",image : #imageLiteral(resourceName: "mark1")),] 



} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return arrayofCellData.count 
} 



override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 



    if arrayofCellData[indexPath.row].cell == 1{ 


     let cell = Bundle.main.loadNibNamed("XibCustomCellJared", owner: self, options: nil)?.first as! XibCustomCellJared 

     cell.ProfilePictureImageView.image = arrayofCellData[indexPath.row].image 
     cell.MoreOptionsImageView.image = arrayofCellData[indexPath.row].image 
     cell.AccountTypeImageView.image = arrayofCellData[indexPath.row].image 
     cell.DisplayNameLabel.text = arrayofCellData[indexPath.row].text 
     cell.StatusSubtitleLabel.text = arrayofCellData[indexPath.row].text 

     return cell 


    }else if arrayofCellData[indexPath.row].cell == 2{ 


     let cell = Bundle.main.loadNibNamed("Xib2CustomCellJared", owner: self, options: nil)?.first as! Xib2CustomCellJared 

     cell.ProfilePictureImageView.image = arrayofCellData[indexPath.row].image 
     cell.MoreOptionsImageView.image = arrayofCellData[indexPath.row].image 
     cell.AccountTypeImageView.image = arrayofCellData[indexPath.row].image 
     cell.DisplayNameLabel.text = arrayofCellData[indexPath.row].text 
     cell.StatusSubtitleLabel.text = arrayofCellData[indexPath.row].text 

     return cell 

    }else{ 




      let cell = Bundle.main.loadNibNamed("XibCustomCellJared", owner: self, options: nil)?.first as! XibCustomCellJared 

      cell.ProfilePictureImageView.image = arrayofCellData[indexPath.row].image 
      cell.MoreOptionsImageView.image = arrayofCellData[indexPath.row].image 
      cell.AccountTypeImageView.image = arrayofCellData[indexPath.row].image 
      cell.DisplayNameLabel.text = arrayofCellData[indexPath.row].text 
      cell.StatusSubtitleLabel.text = arrayofCellData[indexPath.row].text 

      return cell 



     } 

} 
+0

你需要在每個對象中添加3幅不同的圖像,如果你想顯示在每個單元imageviews不同的圖像。 –

+0

我真的還是不明白你的困難......你爲什麼不在你的struct cell中使用3張圖DataData 每個imageview有一個? –

+0

感謝@ClaudioCastro只是因爲我現在明白了!美麗 – benpalmer661

回答

1

編輯:我無緣冠軍,你說你犯了一個定製的視圖單元格。無論哪種方式,如果您使用故事板,下面的方法可能適用於您。

有您創建了一個表格視圖單元格專門爲您的tableview?我認爲你可以通過這種方式更輕鬆地完成你要去的事情。

表視圖單元格(在您的tableview中標識爲原型單元格),您可以創建三個單獨的圖像視圖併爲每個視圖賦予一個唯一標識符(標籤),如下圖所示。當你創建一個表格視圖單元格時,你也必須給視圖單元格一個唯一的標識符。

表視圖小區識別 enter image description here

特定圖像查看標籤標識(可以是任何INT) enter image description here

一旦你有你的表視圖細胞識別,爲你的形象的看法標籤號碼,我們可以得到一些代碼。

裏面你cellForRowAt indexPath功能,您將創建代碼,類似於如下所示:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    //the .dequeReusableCell method creates the instance for the table 
    //view cell you created in the storyboard. Make sure you copy the 
    //identifier exactly as it appears in the storyboard.  

    let cell = tableView.dequeueReusableCell(withIdentifier: "whateverCellIdentifierYouChoose", for: indexPath) 

    //The .viewWithTag method creates the instance for the objects 
    //you created in the table view cell. Copy the tag number 
    //exactly as you specified it in the storyboard. 

    let bananaImage = cell.viewWithTag(1) as! UIImageView 
    let appleImage = cell.viewWithTag(2) as! UIImageView 
    let pearImage = cell.viewWithTag(3) as! UIImageView 

    //code below specifies the image you are going to populate 
    //each UIImageView with 

    bananaImage.image = UIImage(named: "banana") 
    appleImage.image = UIImage(named: "apple") 
    pearImage.image = UIImage(named: "pear") 

    return cell 
} 

試試這個,看看它是否爲你。

0

你在你的模型創建單元格數據。
樣品:

cellData{ 
    var image1:UIImage; // apple 
    var image2:UIImage; //pear 
    var image3:UIImage; // banana 
    var text:String; 
    .... 
} 

cellforrowatindexpath獲取數據:

let data = arrayofCellData[indexPath.row] as! cellData 
cell.ProfilePictureImageView.image = data.image1 
cell.ProfilePictureImageView.image = data.image1 
cell.ProfilePictureImageView.image = data.image1