简体   繁体   English

迅速-如何在自定义视图单元格中显示两个不同的图像

[英]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? 我的视图单元格中有三个imagesviews,它们当前都显示相同的图像,我想知道如何将每个图像制作成单独的图像,例如,如果image = imagebanana显示香蕉图像,或者if image = 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



        }

}

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) 特定图像视图的标签标识符(可以是任何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: 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
}

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: 并在cellforrowatindexpath获取数据:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将多个图像添加到Swift iOS中的自定义表格视图单元格? - How to add multiple images to to custom table view cell in swift ios? 我想使用两个按钮选择图像,并想在两个不同的图像视图中快速显示这些图像。我该怎么做? - I want to select image using two buttons and want to display those images on two different image view in swift..how could I do this.? 如何在 Swift 中的表格视图单元格的右侧添加图标(或自定义图像) - How to add icons(or custom images) to right side of table view cell in Swift 如何在表视图单元格上加载不同的图像 - How to Load Different Images On Table View Cell Swift:如何将单元格视图设计为两个三角形? - Swift: How to design cell view as two triangles? 按下按钮后迅速在下一视图中显示不同的图像吗? - Display different images in next view when button pressed swift? 如何在两个不同部分的两个不同NSMutableArray中的单元格中显示内容(两个标签和一个图像视图)。 - How to display contents(two labels and one image view) in cell from two different NSMutableArray in two different sections. 迅捷:Table View Cell执行到两个不同目的地的隔离 - Swift: Table View Cell perform segue to two different destinations 如何以编程方式将不同单元格的不同图像添加到tableView(Swift 3) - How to add different images for different cell to a tableView programmatically (Swift 3) 如何在表格视图单元格内快速加载两个不同的布局 - How do I load two different layouts inside a table view cell in swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM