i创建了两个不同的单元格以显示其中的不同数据。然后我将reuseIdentifier
("TableCell"
和"FixtureCell"
(分配给他们。他们有两个不同的类,名为FixtureTableViewCell
和LeagueTableViewCell
如果单元格标识符是 "TableCell"
显示 TableCell
。
其他单元标识符是"FixtureCell"
,请显示FixtureCell
。
我该如何控制?
我的代码如下。我只做"TableCell"
。我不能为另一个做。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tblLeagueTable.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as! LeagueTableViewCell
let teams = tableArray[indexPath.row]
cell.lblLeagueTableTeam.text! = teams.teamName
cell.lblOwnGoal.text! = teams.ownGoal
cell.lblScoredGoal.text! = teams.scoredGoal
cell.lblLeagueTableTotalMatch.text! = teams.totalMatch
cell.lblTotalPoints.text! = teams.totalPoint
return cell
}
根据以前的经验,基于Indexpath以实现动态样式不是一个好主意。我想建议模型驱动的计划。
首先,定义这样的模型:
class DemoCellModel: NSObject {
var identifier: String = ""
var classTypeString: String?
var cellData: NSObject?
convenience init(identifier: String, classTypeString: String? = nil, cellData: NSObject? = nil) {
self.init()
self.identifier = identifier
self.classTypeString = classTypeString
self.cellData = cellData
}
}
然后创建一个模型数组:
var models: [DemoCellModel] = []
override func viewDidLoad() {
super.viewDidLoad()
guard var bundleName = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String else { return }
bundleName = bundleName + "."
models.append(DemoCellModel(identifier: "TableCell", classTypeString: bundleName + "LeagueTableViewCell", cellData: nil))
models.append(DemoCellModel(identifier: "FixtureCell", classTypeString: bundleName + "FixtureTableViewCell", cellData: nil))
}
最后,生成您的自定义单元格:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return models.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = models[indexPath.row]
var cell = tableView.dequeueReusableCell(withIdentifier: model.identifier)
if cell == nil {
var cellClassType: UITableViewCell.Type
if let classTypeString = model.classTypeString, let classType = (NSClassFromString(classTypeString) as? UITableViewCell.Type) {
cellClassType = classType
} else {
cellClassType = UITableViewCell.self
}
cell = cellClassType.init(style: .default, reuseIdentifier: model.identifier)
}
if var cell = cell as? testProtocol {
cell.cellData = model.cellData
}
return cell!
}
protocol testProtocol {
var cellData: NSObject? { get set }
}
基于indexPath
,您决定要显示的合适单元格,因此您的代码应该看起来像这样:
var cell: UITableViewCell?
if isTableViewCell(indexPath) {
cell = tblLeagueTable.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as! LeagueTableViewCell
let teams = tableArray[indexPath.row]
cell.lblLeagueTableTeam.text! = teams.teamName
cell.lblOwnGoal.text! = teams.ownGoal
cell.lblScoredGoal.text! = teams.scoredGoal
cell.lblLeagueTableTotalMatch.text! = teams.totalMatch
cell.lblTotalPoints.text! = teams.totalPoint
} else {
cell = tblLeagueTable.dequeueReusableCell(withIdentifier: "FixtureCell", for: indexPath) as! FixtureTableViewCell
// Customize your FixtureCell here
}
我的单元仅在config
中实现CListTableViewCellProtocol
:
protocol CListTableViewCellProtocol {
mutating func config(data: MyData, callback: @escaping (_ value: Double) -> Void)
}
我已经将所有特定内容封装在功能中,然后它直接变为:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = getCellStyleForSection(indexPath.section).cellIdentifier()
var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! CListTableViewCellProtocol
cell.config(data:getDataFromIndexPath(indexPath),
callback: {
(value: Double) -> Void in
self.setCValue(indexPath: indexPath, value: value))
}
)
return cell as! UITableViewCell
}
/// And a function to register the cells
func registerCells(tableView: UITableView) -> Void {
self.tableView = tableView
tableView.register(CListTableViewCell.self,
forCellReuseIdentifier: CCellStyle.editable.cellIdentifier())
tableView.register(CListTableViewCellSmall.self,
forCellReuseIdentifier: CCellStyle.small.cellIdentifier())
}
和CCellStyle
只是一个枚举:
enum CCellStyle:String {
case small = "CellSmall"
case editable = "CellEditable"
func height() -> Int {
switch self {
case .small:
return 20
case .editable:
return 36
}
}
func cellIdentifier() -> String {
return self.rawValue
}
}
尝试以下:
使用不同的标识符作为表部分。您也可以使用 index.row 。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier: String = "TableCell"
let fixtureIdentifier: String = "FixtureCell"
var cell: UITableViewCell?
cell = tableView.dequeueReusableCell(withIdentifier: indexPath.section == 0 ? identifier : fixtureIdentifier)
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: indexPath.section == 0 ? identifier : fixtureIdentifier)
}
if indexPath.section == 0 {
// Do code for TableCell
}
else if indexPath.section == 1 {
// Do code for FixtureCell
}
return cell!
}