2014-12-08 40 views
0

我在UITableViewCell中插入UITableView时遇到问题。这是我的自定义单元格: enter image description here 一切工作正常的第一,第二和第三个单元格,但从第四个单元格中插入单元格的UITableView的内容总是交替使用第一,第二和第三个单元格的相同行。相反,UITableView外部的标签正确显示在每个单元格中。这是我的自定义单元格类代码:在自定义UITableViewCell中包含的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 
    } 

一些建议来解决这个问题?

谢谢你们!

+0

它看起来像你试图显示一个问题与下面的可能的答案列表? 您是否在屏幕上一次显示多个问题? – 2014-12-08 04:00:12

+0

是的,完全如您所说。当视图出现时,仅显示第一个问题,部分显示第二个问题。谢谢! – 2014-12-08 08:12:34

回答

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得到的答案是一个好主意。这取决于获得它们的代价是多么昂贵 - 以上代码将被称为很多。

编辑:我个人喜欢.Grouped风格的表视图。使用每个问题的部分将很好地将其分开。

+0

太棒了!托马斯穆勒你是一个伟大的人!你的代码完美地工作。我缓存了答案,不仅提高了执行速度,甚至因为每次都返回随机排序的答案,所以可能会出现同一个答案可能出现在问题下的多行上。非常感谢你,非常感谢你!编辑:'.Grouped'对于TableView非常棒! – 2014-12-09 11:43:54

+0

我很高兴我可以帮助:-) – 2014-12-09 21:40:14

相关问题