简体   繁体   中英

swift - how to display two different images in a custom made view cell

I have three imagesviews in my view cell they currently all display the same image i would like to know how to make each image a seperate image, maybe something like if image = imagebanana display image of banana else if image = imageapple display image of apple, as at the moment all images in each individual cell are banana , banana, banana and i wanted to have banana, apple , pear in the one cell?

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



        }

}

Edit: I missed the title where you said you made a custom made view cell. Either way, the approach below would probably work for you if you're using storyboards.

Have you created a Table View Cell specifically for your tableview? I think you might be able to accomplish what you're going for more easily that way.

In the Table View Cell (identified as a prototype cell in your tableview), you can create the three separate image views and give each one a unique identifier (tag) as shown in the image below. When you create a table view cell, you have to give the view cell a unique identifier as well.

Table View Cell identifier 在此处输入图片说明

Tag identifier for specific image view (can be any int) 在此处输入图片说明

Once you have your table view cell identified and you give your image views tag numbers, we can get to some code.

Inside of your cellForRowAt indexPath function, you would create code that is similar to what is shown below:

    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
}

Try this and see if it works out for you.

You create data for cell in your model.
Sample:

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

and in cellforrowatindexpath get data:

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

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