【问题标题】:Display the same content in UITableView included in custom UITableViewCell在自定义 UITableViewCell 中包含的 UITableView 中显示相同的内容
【发布时间】:2014-12-09 09:05:30
【问题描述】:

我在 UITableViewCell 中插入 UITableView 时遇到问题。这是我的自定义单元格: 第一个、第二个和第三个单元格一切正常,但是从第四个单元格开始,插入单元格的 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
    }

解决此问题的一些建议?

谢谢你们!

【问题讨论】:

  • 您似乎正试图在下面显示一个带有可能答案列表的问题?您是否一次在屏幕上显示多个问题?
  • 是的,正如你所说。当视图出现时,仅完全显示第一个问题,部分显示第二个问题。谢谢!

标签: ios uitableview swift custom-cell


【解决方案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 样式。每个问题使用一个部分可以很好地在视觉上将它们分开。

【讨论】:

  • 太棒了!托马斯穆勒,你是一个伟大的人!您的代码完美运行。我缓存了答案,不仅是为了提高执行速度,而且因为getAnswersForQuestion() 每次都会返回随机排序的答案,因此可能会出现相同的答案可能出现在问题下的多行中。非常感谢,真的非常感谢!编辑:.Grouped 非常适合 TableView!
  • 很高兴能帮上忙 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-09
  • 1970-01-01
  • 2014-09-05
  • 2014-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多