[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 的委托(delegate)的一部分,顺便说一句,我将 Abhinav 的 inits 复制粘贴到我的代码中,但这些 inits 再次不起作用。 (没有输出到控制台)
最佳答案
我不确定我是否正确理解了您的问题,但它似乎与类之间的继承有关。所以基本上你有一个继承自“UITableViewCell”的“GenericCell”类,以及每个继承自“GenericCell”的“CellOne”、“CellTwo”和“CellThree”类。如果你想通过 init with style,一种设置方法如下:
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)
}
}
然后您可以像这样在 TableView 的数据源中创建 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 - swift 。正确初始化 UITableViewCell 层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33317158/