首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据reuseIdentifier控制我的手机?

如何根据reuseIdentifier控制我的手机?
EN

Stack Overflow用户
提问于 2017-06-24 10:13:42
回答 4查看 752关注 0票数 0

我创建了两个不同的单元格来显示不同的数据。我给他们分配了reuseIdentifier ("TableCell""FixtureCell")。它们有两个不同的类,分别名为FixtureTableViewCellLeagueTableViewCell

如果单元格标识符是"TableCell"显示TableCell,我想这样做

else如果单元格标识符为"FixtureCell",则显示FixtureCell

如何进行此控制?

我的代码如下。我只做了"TableCell"。我不能为另一个人做。

提前谢谢。

代码语言:javascript
复制
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

}
EN

回答 4

Stack Overflow用户

发布于 2017-06-24 13:33:14

根据以前的经验,基于IndexPath实现动态样式不是一个好主意。我想建议模型驱动的计划。

首先,像这样定义一个模型:

代码语言:javascript
复制
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
    }
}

然后,创建一个模型数组:

代码语言:javascript
复制
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))
}

最后,生成自定义单元格:

代码语言:javascript
复制
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 }
}
票数 1
EN

Stack Overflow用户

发布于 2017-06-24 10:22:35

根据indexPath决定要显示的合适单元格,因此您的代码应该如下所示:

代码语言:javascript
复制
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
}
票数 0
EN

Stack Overflow用户

发布于 2017-06-24 10:24:43

我的单元实现了一个CListTableViewCellProtocol,其中只有一个config

代码语言:javascript
复制
protocol CListTableViewCellProtocol{
    mutating func config(data: MyData, callback: @escaping (_ value: Double) -> Void)
}

我已经将所有特定的东西封装在函数中,然后它就变得简单了:

代码语言:javascript
复制
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 funciton 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只是一个枚举:

代码语言:javascript
复制
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
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44732091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档