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

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

概述[UITableViewCell]< - [genericCell]< - [Cell1],[Cell2],[Cell3] 你好.请想象上面的等级.在我的代码中,我没有完全类型为genericCell的对象,但是这个类共享一些属性. 我的代码应该包含哪些设计?我有genericCell的以下结构: override init(style: UITableViewCellStyle, reuseId [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代表的一部分,顺便说一下,我将Abhinav的内容复制粘贴到我的代码中,而且这些内容无法正常工作. (没有输出到控制台)

解决方法 我不确定我是否理解你的问题,但它似乎是关于类之间的继承.所以基本上你有一个“GenericCell”类,它继承自“UItableVIEwCell”,“CellOne”,“CellTwo”和“CellThree”类,每个类都继承自“GenericCell”.如果你想通过样式进行init,设置它的一种方法是这样的:
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: 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的实例,如下所示:

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 – 斯威夫特.正确初始化UITableViewCell层次结构全部内容,希望文章能够帮你解决ios – 斯威夫特.正确初始化UITableViewCell层次结构所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1105308.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存