Swift Proper initialization of the UITableViewCell hierarchy

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

Hello. Imagine the hierarchy above. In my code, I do not have objects of type genericCell , but this class has some properties.

What design should be in my code? I have the 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) } 

But what about Cell1 ? How can I call init(style: UITableViewCellStyle, reuseIdentifier: String?) In genericCell for "my stuff" operations through initializing an instance of Cell1 ? Now they are not executed.


EDIT

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

Hey. This is part of the tableView delegate, by the way, I copied Abhinav into my code and again these inits do not work. (no console output)

+7
ios xcode uitableview swift
source share
2 answers

I'm not sure I understood your question correctly, but it seems to be about inheritance between classes. So basically you have the "GenericCell" class, which inherits from the "UITableViewCell" and "CellOne", "CellTwo" and "CellThree" classes, each of which inherits from "GenericCell". If you want to go through the init style, one way to set this would be as follows:

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

Then you could create CellOne instances in the table view data source as follows:

 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, the usual setup made in "GenericCell" will go through first, and then through the unique setup in "CellOne". "CellTwo" and "CellThree" will be configured accordingly.

EDIT

A more specific 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! } 
+11
source share

Here is what I would call your hierarchy:

Step 1: Make The Generic Cell Class

 class GenericCell : UITableViewCell { override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) print("Generic Cell Initialization Done") } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } } 

Step 2: Make a specific cell class 1:

 class MyCell1 : GenericCell { override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) print("MyCell1 Initialization Done") } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } } 

Step 3: Make a specific cell class 2:

 class MyCell2 : GenericCell { override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) print("MyCell2 Initialization Done") } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } } 

Step 4: Make a specific cell of class 3:

 class MyCell3 : GenericCell { override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) print("MyCell3 Initialization Done") } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } } 

Step 5: Finally, use the following cells:

 let cell1 = MyCell1.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell1") let cell2 = MyCell2.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell2") let cell3 = MyCell3.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell3") 

PS: This would guarantee the setting of properties in a common cell, as well as in certain cells.

EDIT: This is how you would use cells in cellForRowAtIndexPath :

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if indexPath.section == 0 { let cell1 = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as MyCell1 if cell1 == nil { cell1 = MyCell1.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell1") } // Do your cell property setting return cell1 } else if indexPath.section == 1 { let cell2 = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as MyCell2 if cell2 == nil { cell2 = MyCell2.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell2") } // Do your cell property setting return cell2 } else { let cell3 = tableView.dequeueReusableCellWithIdentifier("cell3", forIndexPath: indexPath) as MyCell3 if cell3 == nil { cell3 = MyCell3.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell3") } // Do your cell property setting return cell3 } } 
+2
source share

All Articles