dre*_*pts 8 xcode uitableview ios swift
[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)
}
Run Code Online (Sandbox Code Playgroud)
但那怎么样Cell1?我怎样才能调用init(style: UITableViewCellStyle, reuseIdentifier: String?)中genericCell通过的初始化"我的东西"的操作Cell1实例?现在他们没有表演.
编辑
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)
}
//...
Run Code Online (Sandbox Code Playgroud)
你好,我们又见面了.这是tableView的委托的一部分,顺便说一句,我将Abhinav的内容复制粘贴到我的代码中,而且这些内容无法正常工作.(没有输出到控制台)
Gam*_*mma 13
我不确定我是否理解你的问题,但它似乎是关于类之间的继承.所以基本上你有一个"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: 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)
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以在表视图的数据源中创建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!
}
Run Code Online (Sandbox Code Playgroud)
对于每个实例,它现在将首先完成在"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!
}
Run Code Online (Sandbox Code Playgroud)
这就是我要确定您提到的层次结构的方式:
第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)
}
}
Run Code Online (Sandbox Code Playgroud)
第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)
}
}
Run Code Online (Sandbox Code Playgroud)
步骤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)
}
}
Run Code Online (Sandbox Code Playgroud)
步骤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)
}
}
Run Code Online (Sandbox Code Playgroud)
步骤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")
Run Code Online (Sandbox Code Playgroud)
PS:这将确保在通用单元格以及特定单元格中设置属性。
编辑:这是您将如何使用单元格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
}
}
Run Code Online (Sandbox Code Playgroud)