是否可以将自定义UITableViewCell存储到Array中? [英] Is it possible to store custom UITableViewCell into Array?
问题描述
我有很多自定义单元格,并且想要通过使用struct并将有关单元格的信息存储在数组中来简化 cellForRowAt
.
I have a lot of custom cells, and wanted to streamline the cellForRowAt
by using the struct and storing information about the cell in an array.
struct HowToCells {
let helpCell: UITableViewCell
let identifier: String
let icon: UIImage
let cellName: String
}
var cellArray = [HowToCells]()
cellArray.append(HowToCells.init(helpCell: ScreenRecordingTableViewCell(), identifier: "ScreenRecordingTableViewCell", icon: HowToImage.screenRecording.image(), cellName: "Enable screen recording"))
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let c = cellArray[indexPath.row]
let cellToUse = c.helpCell
let cell = tableView.dequeueReusableCell(withIdentifier: c.identifier, for: indexPath) as! cellToUse
}
我收到以下错误:使用未声明的类型'cellToUse'
I get the following error: Use of undeclared type 'cellToUse'
ScreenRecordingTableViewCell
是自定义的UITableViewCell
ScreenRecordingTableViewCell
is a custom UITableViewCell
推荐答案
您不需要将表视图单元格存储在数组中,因为UITableView会自行创建和存储它们.只需在 tableView(_:cellForRowAt:)
中使用 tableView.dequeueReusableCell(withIdentifier:for:)
方法来获取所需的单元格.之后,您可以配置此单元格.
You don't need to store table view cells in array, because UITableView creates and stores them by itself. Just use tableView.dequeueReusableCell(withIdentifier:for:)
method in tableView(_:cellForRowAt:)
to get needed cell. After that you can configure this cell.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! CellClass
// Configure cell
return cell
}
这篇关于是否可以将自定义UITableViewCell存储到Array中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!