gpt4 book ai didi

ios - 具有不同数据的 TableView 自定义单元格

转载 作者:行者123 更新时间:2023-12-02 04:04:51 26 4
gpt4 key购买 nike

所以,我想将不同的图像放入表格 View 单元格中,并且我有 3 个不同的文件。我已经做了一些编码,但现在完成它时遇到问题。请看看我的代码并告诉我如何处理这个问题,我对此很陌生。 (卡了一整天)...这样做的全部目的是拥有具有不同图像和两个标签的自定义单元格。

First file

class MenuCells {

private var _cellTitle: String
private var _cellDetails: String
private var _cellIcon: Array<String>

var cellTitle: String {
return _cellTitle
}

var cellDetails: String {
return _cellDetails
}

var cellIcon: Array<String> {
return _cellIcon
}

init(cellTitle: String, cellDetails: String, cellIcon: Array<String>) {

_cellTitle = cellTitle
_cellDetails = cellDetails
_cellIcon = cellIcon

}

func cellData() {
_cellTitle = "x"
_cellDetails = "y"
_cellIcon = ["1","2","3","4"]
}

}

Second File

class MenuCell: UITableViewCell {


@IBOutlet weak var bg: UIView!
@IBOutlet weak var cellTitle: UILabel!
@IBOutlet weak var cellDetails: UILabel!
@IBOutlet weak var cellIcon: UIImageView!


override func awakeFromNib() {
super.awakeFromNib()


}

func configureCell(menuCell: MenuCells) {

cellTitle.text = menuCell.cellTitle
cellDetails.text = menuCell.cellDetails
cellIcon.image = UIImage(named: "\(menuCell.cellIcon)")

}
}

Third File (Here Im stuck, I dont know how implement the data)

import UIKit

class MenuVC: UIViewController, UITableViewDelegate, UITableViewDataSource {


@IBOutlet weak var tableMenu: UITableView!



override func viewDidLoad() {
super.viewDidLoad()

tableMenu.delegate = self
tableMenu.delegate = self



}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "menuCell", for: indexPath) as? MenuCell


return cell
}


}

最佳答案

你需要做这样的事情。

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "menuCell", for: indexPath) as? MenuCell

// create object of MenuCells file and assign your variables.Instead of creating array of images in MenuCell, create array in this file and send image as a param 1-by-1.
let myMenuCells = MenuCells(cellTitle: "Title", cellDetails: "Details", cellIcon: image)

//By this below method set your data in your outlets. As this func is already doing so call it.
cell.configureCell(menuCell: myMenuCells)

return cell
}

并改变

  • private var _cellIcon: Array<String>private var _cellIcon: String因为您只需发送图像名称和 func configureCell自动将图像分配给您的 socket 。

  • cellIcon.image = UIImage(named: "\(menuCell.cellIcon)")cellIcon.image = UIImage(named: menuCell.cellIcon)

关于ios - 具有不同数据的 TableView 自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41979610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com