在自定义UITableViewCell中包含的UITableView中显示相同的内容

时间:2014-12-08 00:14:10

标签: ios uitableview swift custom-cell

我在UITableViewCell中插入UITableView时遇到问题。这是我的自定义单元格: enter image description here 对于第一个,第二个和第三个单元格,一切正常,但是从第四个单元格中插入单元格中的UITableView的内容总是使用第一个,第二个和第三个单元格的相同行。相反,UITableView外部的标签会在每个单元格中正确显示。这是我自定义单元类的代码:

import UIKit

class SimulazioneQuestionTableViewCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {

    let cellIdentifier = "simRisposteCell"
    var arrayRisposte = [Risposta]()

    @IBOutlet var numeroLabel: UILabel!
    @IBOutlet var domandaLabel: UILabel!
    @IBOutlet var risposteTableView: UITableView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        var nib = UINib(nibName: "SimulazioneQuestionAnswerTableViewCell", bundle: nil)
        self.risposteTableView.registerNib(nib, forCellReuseIdentifier: self.cellIdentifier)
    }

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

        // Configure the view for the selected state
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrayRisposte.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as SimulazioneQuestionAnswerTableViewCell

        cell.textLabel?.text = arrayRisposte[indexPath.row].testo

        return cell
    }

}

这是我的视图控制器:

import UIKit


class SimulazioneViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var areaSimulazione: Int = Int()
    var arrayDomande = [Domanda]()
    var arrayRisposte = [[Risposta]]()
    var cellIdentifier = "domandaSimulazioneCell"


    @IBOutlet var tempoTrascorso: UILabel!
    @IBOutlet var simulazioneTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        arrayDomande = ModelManager.instance.getSimulazione(area: areaSimulazione)

        var nib = UINib(nibName: "SimulazioneQuestionTableViewCell", bundle: nil)
        self.simulazioneTableView.registerNib(nib, forCellReuseIdentifier: self.cellIdentifier)

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrayDomande.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: SimulazioneQuestionTableViewCell = simulazioneTableView.dequeueReusableCellWithIdentifier(self.cellIdentifier) as SimulazioneQuestionTableViewCell
        var domanda: Domanda = arrayDomande[indexPath.row]
        var rispXDomanda = ModelManager.instance.getAnswersForQuestion(domanda.numero)
        cell.numeroLabel.text = String(domanda.numero)
        cell.domandaLabel.text = domanda.testo
        cell.arrayRisposte = rispXDomanda

        return cell
    }

解决此问题的一些建议?

谢谢你们!

1 个答案:

答案 0 :(得分:1)

我从来没有想过将UITableView置于UITableViewCell内,因此会从不同的方向接近这一点。我并不是说单元格中的表格是不可能的或者是个坏主意,但以下方法实际上可能更容易。

我的理解是,您的表格视图显示了一些问题,每个问题都在其下方有可能的答案。

我会在每个问题中使用一个部分,第一行使用自定义单元格显示numeroLabeldomandaLabel(基本上SimulazioneQuestionTableViewCell没有表格),然后放入该部分的剩余行的答案。

private let QuestionCellIdentifier = "QuestionCell"
private let AnswerCellIdentifier = "AnswerCell"

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  return arrayDomande.count
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  let rispXDomanda = ModelManager.instance.getAnswersForQuestion(domanda.numero)
  return 1 + rispXDomanda.count // one extra for the question
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  let domanda = arrayDomande[indexPath.section]

  switch indexPath.row {
  case 0:
    let cell = tableView.dequeueReusableCellWithIdentifier(QuestionCellIdentifier) as SimulazioneQuestionTableViewCell
    cell.numeroLabel.text = String(domanda.numero)
    cell.domandaLabel.text = domanda.testo
    return cell
  default:
    let cell = tableView.dequeueReusableCellWithIdentifier(AnswerCellIdentifier) as SimulazioneQuestionAnswerTableViewCell
    let rispXDomanda = ModelManager.instance.getAnswersForQuestion(domanda.numero)
    cell.textLabel?.text = rispXDomanda[indexPath.row - 1].testo
    return cell
  }

缓存从ModelManager获得的答案可能是个好主意。这取决于获取它们的成本是多少 - 上面的getAnswersForQuestion()代码会被调用很多。

编辑:我个人喜欢桌面视图的.Grouped样式。每个问题使用一个部分可以很好地将它们视觉分开。

相关问题