繁体   English   中英

将 tableview 添加到 tableview 单元格时出现问题 swift

[英]Problem with adding tableview into tableview cell swift

我想显示带有单元格的表格视图,其中将是一个 label 和此 label 下面的表格视图。我现在有这样的布局:

在此处输入图像描述

在某些情况下,我的表格视图上方的视图将被隐藏,因此我将在整个屏幕上显示表格视图。 所以...我找到了这个视频,开发人员设法解决了我的任务。 我做了与他的视频类似的所有事情,但我没有设法在表格视图单元格内显示表格视图。 这是我的步骤:

  1. 将所有视图添加到一般视图。
  2. 将标签附加到我的表格视图:主表格视图 100 个,内部表格视图 90 个。
  3. 创建单元格类并将它们附加到两个单元格。
  4. 像视频中那样在主单元格添加扩展。
  5. 在主视图中处理表视图标记 controller。

如我所见,问题出在无法显示的内部表格视图中。 您可以在下面看到单元格的类。 主电池:

class GeneralCell : UITableViewCell {
    @IBOutlet weak var answerOptions: UITableView!
    @IBOutlet weak var questionText: UILabel!
}

extension GeneralCell{
    func setTableViewDataSourceDelegate <D:UITableViewDelegate & UITableViewDataSource> (_ dataSourceDelegate: D, forRow row: Int)
    {
        answerOptions.delegate = dataSourceDelegate
        answerOptions.dataSource = dataSourceDelegate
        
        answerOptions.reloadData()
    }
}

内部单元格:

class AnswersCell: UITableViewCell {
    @IBOutlet weak var answerOption: UILabel!
}

这是我的观点 controller:

class PollsController: UIViewController, UITableViewDataSource,UITableViewDelegate {

    @IBOutlet weak var questionTableView: UITableView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        questionTableView.delegate = self
        questionTableView.dataSource = self
        
    }
    

    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print(tableView.tag)
        if tableView.tag == 100 {
            let cell: GeneralCell = tableView.dequeueReusableCell(withIdentifier: "GeneralCell") as! GeneralCell
            cell.answerOptions.reloadData()
            return cell
        }else{
            let cell: AnswersCell = tableView.dequeueReusableCell(withIdentifier: "AnswersCell") as! AnswersCell
            return cell
        }
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        if tableView.tag == 100 {
            return 1
        }else{
            return 6
        }
    }
}

然后我做了一些测试以了解问题出在哪里。 我将print()添加到数据加载器 function 中:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print(tableView.tag)
        if tableView.tag == 100 {
            print("1")
            let cell: GeneralCell = tableView.dequeueReusableCell(withIdentifier: "GeneralCell") as! GeneralCell
            cell.answerOptions.reloadData()
            return cell
        }else{
            print("2")
            
            let cell: AnswersCell = tableView.dequeueReusableCell(withIdentifier: "AnswersCell") as! AnswersCell
            return cell
        }
    }

而在 output 中我只看到了一个标签:

100

正如我在表视图中看到的那样无法加载,但我不明白为什么。 也许你们中有人会发现问题或错误?

我建议你只使用一个 tableview,这里有一些例子

struct Answer {
    let answerText: String
}

struct Question {
    let questionText: String
    let answers: [Answer]
}

class ViewController: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!
    var questions: [Question] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        for i in 1...10 {
            let question = Question(questionText: "Question \(i)", answers: [
                Answer(answerText: "Answer 1 Question \(i)"),
                Answer(answerText: "Answer 2 Question \(i)"),
                Answer(answerText: "Answer 3 Question \(i)"),
                Answer(answerText: "Answer 4 Question \(i)")
            ])
            questions.append(question)
        }
        tableView.dataSource = self
        tableView.delegate = self
        tableView.sectionHeaderHeight = UITableView.automaticDimension;
        tableView.estimatedSectionHeaderHeight = 44
    }
    
    
}

extension ViewController: UITableViewDataSource, UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        return questions.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return questions[section].answers.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "AnswerCell", for: indexPath) as! AnswerCell
        
        cell.answerLabel.text = questions[indexPath.section].answers[indexPath.row].answerText
        return cell
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let cell = tableView.dequeueReusableCell(withIdentifier: "QuestionCell") as! QuestionCell
        
        cell.questionLabel.text = questions[section].questionText
        return cell
    }
}

在此处输入图像描述

试试这个

class GeneralCell : UITableViewCell {
    @IBOutlet weak var answerOptions: UITableView!
    @IBOutlet weak var questionText: UILabel!
}



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print(tableView.tag)
        if tableView == questionTableView{
            let cell: GeneralCell = tableView.dequeueReusableCell(withIdentifier: "GeneralCell") as! GeneralCell
            cell.answerOptions.delegate = self
            cell.answerOptions.dataSource = self
            cell.answerOptions.reloadData()
            return cell
        }else{
            let cell: AnswersCell = tableView.dequeueReusableCell(withIdentifier: "AnswersCell") as! AnswersCell
            return cell
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM