[英]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.