2017-02-21 27 views
0

有人知道如何填写2个不同的单元格吗? enter image description here如何填写2个不同的单元格?

对于每一个细胞我有特定的类

import UIKit 

class MindCell: UITableViewCell { 

    @IBOutlet var textLabel1: UILabel! 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 

    override func setSelected(_ selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 

} 

class StepsMind: UITableViewCell { 

    @IBOutlet var number1Label: UILabel! 
    @IBOutlet var step1Label: UILabel! 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 

    override func setSelected(_ selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 

} 

但我不知道如何设置功能

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cellIdentifier = "Cell" 
     let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! MindCell 

     // Configure the cell... 
     cell.textLabel1.text = "ABC" 

     return cell 
} 
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cellIdentifier1 = "MindCell" 
     let cell1 = tableView.dequeueReusableCell(withIdentifier: cellIdentifier1, for: indexPath) as! StepsMind 

     // Configure the cell... 
     cell1.number1Label.text = "1" 
     cell1.step1Label.text = "DEF" 

     return cell1 
     } 

Xcode中说,这是不可能同时使用2个这些功能时间。我需要做什么?

+0

您如何知道要使用哪个单元格?不同的tableView或相同?只是在相同和独特的'func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - > UITableViewCell',do和'if'测试那里使用一个或另一个。 – Larme

+0

每个单元都有自己的标识符 –

+1

的确,但它们有一个顺序,一个indexPath。显示“MindCell”时以及显示“StepsMind”时的逻辑是什么? – Larme

回答

0

通过查看评论我假设你想要备用行类型。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    { 

    if indexPath.row % 2 == 0 
    { 
     let cellIdentifier = "Cell" 
     let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! MindCell 

     // Configure the cell... 
     cell.textLabel1.text = "ABC" 

     return cell 
    } 
    else 
    { 
     let cellIdentifier1 = "MindCell" 
     let cell1 = tableView.dequeueReusableCell(withIdentifier: cellIdentifier1, for: indexPath) as! StepsMind 

     // Configure the cell... 
     cell1.number1Label.text = "1" 
     cell1.step1Label.text = "DEF" 

     return cell1 

    } 

} 
+0

还有一个问题的功能'覆盖FUNC的tableView(_的tableView:UITableView的,numberOfRowsInSection段中:int) - >诠释{ 回报1 }' 如果我设置'返回1'它正常工作,但我看到只有一个单元格如果我设置'返回2'我的程序不起作用 –

+0

@IuriiUshakov如果你有返回2,那么你还必须切换或为' indexPath.section'给出相关的行 – Honey

相关问题