2017-05-31 46 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