[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!
- }