迅速。正确初始化UITableViewCell层次结构 [英] Swift. Proper initialization of UITableViewCell hierarchy

查看:202
本文介绍了迅速。正确初始化UITableViewCell层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

您好。请想象上面的等级。在我的代码中,我没有完全类型 genericCell 的对象,但是这个类共享一些属性。

Hello. Please imagine hierarchy above. In my code I don't have objects exactly of type genericCell, but this class shares some properties.

什么inits的设计应该在我的代码中?我对genericCell有以下结构:

What design of inits should be in my code? I have following structure for 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)

}

但是小区1 ?如何通过 genericCell 为我的东西操作调用 init(style:UITableViewCellStyle,reuseIdentifier:String?)初始化 Cell1 实例?现在他们没有表现。

But what about Cell1? How can I invoke init(style: UITableViewCellStyle, reuseIdentifier: String?) in genericCell for "my stuff" operations through initialisation of Cell1 instance? Now they doesn't perform.

编辑

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代表的一部分,顺便说一下,我将Abhinav的内容复制粘贴到我的代码中,而且这些内容无法正常工作。 (没有输出到控制台)

Hi again. This is part from tableView's delegate, btw I copy-pasted Abhinav's inits to my code and again those inits aren't working. (no output to console)

推荐答案

我不确定我是否理解你的问题,但它似乎是关于继承之间的继承类。所以基本上你有一个GenericCell类,它继承自UITableViewCell,CellOne,CellTwo和CellThree类,每个类都继承自GenericCell。如果你想通过 init使用样式,设置它的一种方法是这样的:

I'm not sure I understand your question correctly, but it seems to be about inheritance between classes. So basically you have a "GenericCell" class that inherits from "UITableViewCell", and "CellOne", "CellTwo", and "CellThree" classes that each inherit from "GenericCell". If you want to go through init with style, one way to set this up would be like this:

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)
    }
}

然后你可以创建CellOne的实例你的标签le view的数据源如下:

You could then create instances of CellOne in your table view's data source like so:

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将相应设置。

For each instance it will now first go through the common setup done in "GenericCell", and then through the unique setup in "CellOne". "CellTwo" and "CellThree" would be set up accordingly.

编辑

一个更具体的示例,说明如何配置所有三种Cell类型的实例:

A more concrete example of how to configure instances of all three Cell types:

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!
    }

这篇关于迅速。正确初始化UITableViewCell层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆