ios 迅速。UITableViewCell 层次结构的正确初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33317158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift. Proper initialization of UITableViewCell hierarchy
提问by drewpts
[UITableViewCell] <- [genericCell] <- [Cell1], [Cell2], [Cell3]
[UITableViewCell] <- [genericCell] <- [Cell1], [Cell2], [Cell3]
Hello. Please imagine hierarchy above. In my code I don't have objects exactly of type genericCell
, but this class shares some properties.
你好。请想象上面的层次结构。在我的代码中,我没有完全类型为的对象genericCell
,但是这个类共享一些属性。
What design of inits should be in my code? I have following structure for 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)
}
But what about Cell1
? How can I invoke init(style: UITableViewCellStyle, reuseIdentifier: String?)
in genericCell
for "my stuff" operations through initialisation of Cell1
instance? Now they doesn't perform.
但是呢Cell1
?我怎样才能调用init(style: UITableViewCellStyle, reuseIdentifier: String?)
中genericCell
通过的初始化“我的东西”的操作Cell1
实例?现在他们不执行了。
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)
}
//...
Hi again. This is part from tableView's delegate, btw I copy-pasted Abhinav's inits to my code and again those inits aren't working. (no output to console)
你好,我们又见面了。这是 tableView 委托的一部分,顺便说一句,我将 Abhinav 的初始化复制粘贴到我的代码中,但这些初始化再次不起作用。(没有输出到控制台)
回答by Gamma
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:
我不确定我是否正确理解您的问题,但这似乎与类之间的继承有关。所以基本上你有一个从“UITableViewCell”继承的“GenericCell”类,以及从“GenericCell”继承的“CellOne”、“CellTwo”和“CellThree”类。如果你想通过init 使用 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)
}
}
You could then create instances of CellOne in your table view's data source like so:
然后,您可以在表视图的数据源中创建 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!
}
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.
对于每个实例,它现在将首先通过“GenericCell”中完成的通用设置,然后通过“CellOne”中的独特设置。将相应地设置“CellTwo”和“CellThree”。
EDIT
编辑
A more concrete example of how to configure instances of all three Cell types:
如何配置所有三种 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!
}
回答by Abhinav
This is how I would lay down the hierarchy mentioned by you:
这就是我将如何制定您提到的层次结构:
Step 1 :Make Generic Cell class
第 1 步:制作通用单元类
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 Specific Cell 1 class:
第 2 步:制作特定单元格 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 Specific Cell 2 class:
第 3 步:使特定单元格 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 Specific Cell 3 class:
第 4 步:使特定单元格 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 cells like this:
第 5 步:最后使用这样的单元格:
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 setting the properties in generic cell as well in specific cells.
PS:这将保证在通用单元格以及特定单元格中设置属性。
EDIT:This is how you would use cells in cellForRowAtIndexPath
:
编辑:这是您在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
}
}