ios - swift 。正确初始化 UITableViewCell 层次结构

标签 ios xcode swift uitableview

[UITableViewCell] <- [genericCell] <- [Cell1], [Cell2], [Cell3]

你好。请想象上面的层次结构。在我的代码中,我没有完全属于 genericCell 类型的对象,但此类共享一些属性。

我的代码中应该采用什么样的初始化设计?我有以下 genericCell 结构:

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    //my stuff (initializing shared properties)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

}

但是 Cell1 呢?我如何通过 Cell1 实例的初始化调用 genericCell 中的 init(style: UITableViewCellStyle, reuseIdentifier: String?) 进行“我的东西”操作?现在他们没有表现。


编辑

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let typeOfCell = FbDataManager.sharedInstance.posts[indexPath.row][FbDataManager.sharedInstance.typeParameter] as! String

        switch typeOfCell {
            case self.linkTypeOfPost:

                var cell = tableView.dequeueReusableCellWithIdentifier(self.linkCellIdentifier) as? FbLinkPostViewCell
                if cell == nil {
                    cell = FbLinkPostViewCell.init(style: .Default, reuseIdentifier: self.linkCellIdentifier)
                }
//...

你好。这是来自 tableView 的委托(delegate)的一部分,顺便说一句,我将 Abhinav 的 inits 复制粘贴到我的代码中,但这些 inits 再次不起作用。 (没有输出到控制台)

最佳答案

我不确定我是否正确理解了您的问题,但它似乎与类之间的继承有关。所以基本上你有一个继承自“UITableViewCell”的“GenericCell”类,以及每个继承自“GenericCell”的“CellOne”、“CellTwo”和“CellThree”类。如果你想通过 init with style,一种设置方法如下:

class GenericCell: UITableViewCell {
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // code common to all your cells goes here
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

class CellOne: GenericCell {
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier) // the common code is executed in this super call
        // code unique to CellOne goes here
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

然后您可以像这样在 TableView 的数据源中创建 CellOne 实例:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("cell")
    if (cell == nil) {
        cell = CellOne.init(style: .Default, reuseIdentifier: "cell")
    }
    return cell!
}

对于每个实例,它现在将首先通过“GenericCell”中完成的通用设置,然后通过“CellOne”中的独特设置。 “CellTwo”和“CellThree”将相应设置。

编辑

如何配置所有三种 Cell 类型的实例的更具体示例:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        // you need to write a method like this to figure out which type you need:
        let cellID = self.cellIDForIndexPath(indexPath) // returns either "cell1", "cell2" or "cell3"

        // dequeue or init a cell of the approriate type
        var cell = tableView.dequeueReusableCellWithIdentifier(cellID)
        if (cell == nil) {
            switch cellID {
                case "cell1": cell = CellOne.init(style: .Default, reuseIdentifier: "cell")
                case "cell2": cell = CellTwo.init(style: .Default, reuseIdentifier: "cell")
                case "cell3": cell = CellThree.init(style: .Default, reuseIdentifier: "cell")
                default: cell = UITableViewCell()
            }

        }

        // configure the individual cell if needed (you need to implement methods + logic here that fit your data)
        (cell as! GenericCell).configureForData(self.dataForIndexPath(indexPath))

        return cell!
    }

关于ios - swift 。正确初始化 UITableViewCell 层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33317158/

相关文章:

ios - 数据服务错误未解决

ios - UISearchBar 嵌入 UINavigationBar 时宽度不会改变

cocoa - 如何处理 cocoa 中的警告未知转义序列 '\040'

ios - 比较两个相同的 Double 值返回 false Swift 3

ios - 字体在 Storyboard 中有效,但在 iOS 模拟器 (Xcode 8) 中无效

ios - Pod 安装错误 : [! ]无法添加 url 为 `https://github.com/CocoaPods/Specs.git` 的源名为 master

ios - 将信息从一个 Collection View 传递到另一个更详细的 Collection View 的最佳方式是什么

ios - 注册后无法使用 iOS Cognito UserPool SDK 更改用户属性

ios - Admob 插页式广告不显示

ios - 在自定义对象中存储 UIImage 失败