【问题标题】:Swift: How do I assign wilDisplayCell to specific custom register cell?Swift:如何将 wilDisplayCell 分配给特定的自定义寄存器单元?
【发布时间】:2015-07-11 18:20:23
【问题描述】:

我的 tableViewController 中有两个已注册的单元格,但其中一个具有动画功能(显示/隐藏),所以当我注册第二个单元格并将其带到 tableview 时,我在 tableview 的wilDisplayCell 函数中收到错误, 如何告诉 willDisplayCell 只应用一种类型的单元格?

我的 2 个自定义注册单元: //注册自定义单元格

self.tableView.registerNib(UINib(nibName: "NormalPostTableViewCell", bundle: nil), forCellReuseIdentifier: "NormalPostCell")
self.tableView.registerNib(UINib(nibName: "VideoTableViewCell", bundle: nil), forCellReuseIdentifier: "VideoCell")

一种细胞类型的覆盖函数:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        (cell as! NormalPostTableViewCell).watchFrameChanges()
}

错误:

无法将“app.VideoTableViewCell”(XXXXXX) 类型的值转换为 'app.NormalPostTableViewCell' (XXXXXX)。

错误截图:

方法 2:

我尝试的另一种方法(2)是像这样将willDisplayCell cell:设置为NormalPostTableViewCell并删除override

func tableView(tableView: UITableView, willDisplayCell cell: NormalPostTableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        (cell as NormalPostTableViewCell).watchFrameChanges()
}

而方法2导致这个错误:

方法 'tableView(:willDisplayCell:forRowAtIndexPath:)' 与 Objective-C 选择器'tableView:willDisplayCell:forRowAtIndexPath:' 与方法冲突 'tableView(:willDisplayCell:forRowAtIndexPath:)' 来自超类 'UITableViewController' 具有相同的 Objective-C 选择器

如何在不干扰其他单元格的情况下解决此问题,并将此功能仅应用于 NormalPostTableViewCell 类?

【问题讨论】:

    标签: ios xcode swift uitableview


    【解决方案1】:
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    
        if let postCell = cell as? NormalPostTableViewCell  {
           postCell.watchFrameChanges()
        }
    }        
    

    【讨论】:

    • 答案解决了错误,但动画仍然适用于所有单元格,你知道为什么吗?
    • 在 if 语句开始时出现错误“使用未解析的标识符”.. 建议我是否有其他解决方案...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    相关资源
    最近更新 更多