没有显示UITableView的UITableViewCell

时间:2019-05-11 09:04:31

标签: ios swift uitableview

我正在尝试创建一个包含其自己的TableView的自定义UITableViewCell。但是,当我执行该应用程序时,嵌套的TableView根本不会显示。它不是在调用返回可重用单元格的委托函数。 optionsList链接到下图所示的TableView。不知道是什么原因阻止了它出现在屏幕上。通过xib文件创建的MultiChoiceCell需要基本构造一个顶部带有标签的表格视图,以及下方包含一个可滚动表格视图的表格视图,该表格视图包含一个带有简单标签的单元格,这些单元格在选定时带有选中标记。

ViewController中的代码使用需要在名为optionsList的嵌套表视图中加载的标签值设置options数组:

class MultipleChoiceViewCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource{
    @IBOutlet weak var surveyQuestionLabel: UILabel!
    @IBOutlet weak var optionsList: UITableView!{
        didSet{
            self.optionsList.delegate = self
            self.optionsList.dataSource = self
            self.optionsList.rowHeight = 45
            let nibOptionCell = UINib(nibName: "SimpleTableCell", bundle: nil)
            self.optionsList.register(nibOptionCell, forCellReuseIdentifier: "SimpleTableCell")
            self.optionsList.reloadData()
        }
    }
    var options = [String](){
        didSet{
            self.optionsList.reloadData()
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let return_cell = tableView.dequeueReusableCell(withIdentifier: "SimpleTableCell", for: indexPath) as! SimpleTableViewCell
        return_cell.optionLabel.text = options[indexPath.row]
        return return_cell
    }

}

enter image description here

ViewController中的代码:

    override func viewDidLoad() {
        super.viewDidLoad()

        self.surveyQuestionsTableView.delegate = self
        self.surveyQuestionsTableView.dataSource = self

        self.surveyQuestionsTableView.rowHeight = 285
        let nibFreeResponse = UINib(nibName: "FreeResponseCell", bundle: nil)
        surveyQuestionsTableView.register(nibFreeResponse, forCellReuseIdentifier: "freeResponseCell")

        let nibMultiChoice = UINib(nibName: "MultiChoiceCell", bundle: nil)
        surveyQuestionsTableView.register(nibMultiChoice, forCellReuseIdentifier: "multiChoiceCell")
    }
    //return number of cells to display
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return survey_questions["survey"].count
    }

    //generate cell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let survey_question = survey_questions["survey"][indexPath.row]
        if(survey_question["type"] == "freeResponse"){
            let return_cell = tableView.dequeueReusableCell(withIdentifier: "freeResponseCell", for: indexPath) as! FreeResponseViewCell
            return_cell.surveyQuestionLabel.text = survey_question["question"].string!
            return_cell.surveyResponseField.layer.borderWidth = 0.5
            return_cell.surveyResponseField.layer.borderColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)

            return return_cell
        }

        if(survey_question["type"] == "multipleChoice"){
            let return_cell = tableView.dequeueReusableCell(withIdentifier: "multiChoiceCell", for: indexPath) as! MultipleChoiceViewCell
            return_cell.surveyQuestionLabel.text = survey_question["question"].string!
            return_cell.options = survey_question["answers"].arrayObject as! [String]
            DispatchQueue.main.async {
                return_cell.optionsList.reloadData()
            }


            return return_cell
        }

enter image description here

2 个答案:

答案 0 :(得分:0)

尝试调用reloadData() setupTable中的MultipleChoiceViewCell。从所需的init调用setupTable()?(编码器aDecoder:NSCoder):

//...

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style , reuseIdentifier: reuseIdentifier)
    setUpTable()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setUpTable()
}

func setUpTable(){
    self.optionsList.delegate = self
    self.optionsList.dataSource = self
    self.optionsList.rowHeight = 45
    self.addSubview(optionsList)
    self.reloadData()
}

//...

答案 1 :(得分:0)

从您的初始化方法中调用设置函数...

func setUpTable() {
    optionsList.delegate = self
    optionsList.dataSource = self
    optionsList.rowHeight = 45

    // Register your option cell
    let nibOptionCell = UINib(nibName: "AnswerOptionCell", bundle: nil)
        optionsList.register(nibOptionCell, forCellReuseIdentifier: "AnswerOptionCell")

}

与往常一样,使AnswerOptionCell出队。

建议您将重用标识符与单元格类名称保持相同,以免出错。