Swift. Proper initialization of UITableViewCell hierarchy

前端 未结 2 1868
一生所求
一生所求 2021-02-05 16:04

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

Hello. Please imagine hierarchy above. In my code I don\'t have objects exactly of type gener

2条回答
  •  名媛妹妹
    2021-02-05 16:21

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

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

    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.

    EDIT

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

提交回复
热议问题