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

标签 ios xcode swift3 tableview uitableview

所以,我想将不同的图像放入表格 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/

相关文章:

ios - 使用 Swift 3 拉动刷新和 Alamofire

iphone - 为每个用户生成唯一的号码并对数据进行加密

iphone - 在选择时更改 UITableViewCell 布局

iphone - iOS - 旋转 View 显示背景

ios - Swift 3 - 在 View Controller 之间传递变量和函数

iOS Realm 结果<对象> 无法添加结果<项目>

iOS UIActionSheet 作为插件

ios - 如何从一个 viewController 添加多个 Segue

ios - 是否可以增加 UITableView 分隔线的粗细?

ios - (模拟器 iPhone 4s 不可用。)验证期间