ios – 斯威夫特.正确初始化UITableViewCell层次结构

前端之家收集整理的这篇文章主要介绍了ios – 斯威夫特.正确初始化UITableViewCell层次结构前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
[UITableViewCell]< - [genericCell]< - [Cell1],[Cell2],[Cell3] 你好.请想象上面的等级.在我的代码中,我没有完全类型为genericCell的对象,但是这个类共享一些属性. 我的代码应该包含哪些设计?我有genericCell的以下结构:
  1. override init(style: UITableViewCellStyle,reuseIdentifier: String?) {
  2. super.init(style: style,reuseIdentifier: reuseIdentifier)
  3. //my stuff (initializing shared properties)
  4. }
  5.  
  6. required init?(coder aDecoder: NSCoder) {
  7. super.init(coder: aDecoder)
  8.  
  9. }

但Cell1怎么样?如何通过初始化Cell1实例在genericCell中为“我的东西”操作调用init(style:UITableViewCellStyle,reuseIdentifier:String?)?现在他们没有表演.

编辑

  1. override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  2. let typeOfCell = FbDataManager.sharedInstance.posts[indexPath.row][FbDataManager.sharedInstance.typeParameter] as! String
  3.  
  4. switch typeOfCell {
  5. case self.linkTypeOfPost:
  6.  
  7. var cell = tableView.dequeueReusableCellWithIdentifier(self.linkCellIdentifier) as? FbLinkPostViewCell
  8. if cell == nil {
  9. cell = FbLinkPostViewCell.init(style: .Default,reuseIdentifier: self.linkCellIdentifier)
  10. }
  11. //...

你好,我们又见面了.这是tableView代表的一部分,顺便说一下,我将Abhinav的内容复制粘贴到我的代码中,而且这些内容无法正常工作. (没有输出到控制台)

解决方法

我不确定我是否理解你的问题,但它似乎是关于类之间的继承.所以基本上你有一个“GenericCell”类,它继承自“UITableViewCell”,“CellOne”,“CellTwo”和“CellThree”类,每个类都继承自“GenericCell”.如果你想通过样式进行init,设置它的一种方法是这样的:
  1. class GenericCell: UITableViewCell {
  2. override init(style: UITableViewCellStyle,reuseIdentifier: String?) {
  3. super.init(style: style,reuseIdentifier: reuseIdentifier)
  4. // code common to all your cells goes here
  5. }
  6.  
  7. required init?(coder aDecoder: NSCoder) {
  8. super.init(coder: aDecoder)
  9. }
  10. }
  11.  
  12. class CellOne: GenericCell {
  13. override init(style: UITableViewCellStyle,reuseIdentifier: reuseIdentifier) // the common code is executed in this super call
  14. // code unique to CellOne goes here
  15. }
  16.  
  17. required init?(coder aDecoder: NSCoder) {
  18. super.init(coder: aDecoder)
  19. }
  20. }

然后,您可以在表视图的数据源中创建CellOne的实例,如下所示:

  1. override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  2.  
  3. var cell = tableView.dequeueReusableCellWithIdentifier("cell")
  4. if (cell == nil) {
  5. cell = CellOne.init(style: .Default,reuseIdentifier: "cell")
  6. }
  7. return cell!
  8. }

对于每个实例,它现在将首先完成在“GenericCell”中完成的常见设置,然后通过“CellOne”中的唯一设置.将相应地设置“CellTwo”和“CellThree”.

编辑

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

  1. override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  2.  
  3. // you need to write a method like this to figure out which type you need:
  4. let cellID = self.cellIDForIndexPath(indexPath) // returns either "cell1","cell2" or "cell3"
  5.  
  6. // dequeue or init a cell of the approriate type
  7. var cell = tableView.dequeueReusableCellWithIdentifier(cellID)
  8. if (cell == nil) {
  9. switch cellID {
  10. case "cell1": cell = CellOne.init(style: .Default,reuseIdentifier: "cell")
  11. case "cell2": cell = CellTwo.init(style: .Default,reuseIdentifier: "cell")
  12. case "cell3": cell = CellThree.init(style: .Default,reuseIdentifier: "cell")
  13. default: cell = UITableViewCell()
  14. }
  15.  
  16. }
  17.  
  18. // configure the individual cell if needed (you need to implement methods + logic here that fit your data)
  19. (cell as! GenericCell).configureForData(self.dataForIndexPath(indexPath))
  20.  
  21. return cell!
  22. }

猜你在找的iOS相关文章