【问题标题】:Problem with adding tableview into tableview cell swift将表格视图快速添加到表格视图单元格中的问题
【发布时间】:2020-07-29 07:14:26
【问题描述】:

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

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

  1. 将所有视图添加到常规视图。
  2. 在我的表格视图中附加了标签:主表格视图为 100,内部表格视图为 90。
  3. 创建单元类并将它们附加到两个单元。
  4. 在主单元格中添加了扩展,如视频中。
  5. 在主视图控制器中处理表视图标签。

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

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!
}

这是我的视图控制器:

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()

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
        }
    }

在输出中我只看到一个标签:

100

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

【问题讨论】:

  • 为什么在tableview单元格中需要tableview?
  • @aiwiguna,因为我会在这里放一些投票,而内部表格视图将代表答案选项,你为什么要问?
  • 因为不建议使用这种方法,它会在以后产生滚动行为的新问题,我认为这种 UI 只需 1 个 tableview 即可实现,只需使用部分作为问题和单元格作为答案
  • @aiwiguna,我同意你的观点,但我无法想象如何通过一个 tableview 达到我的目标 :( 如果我在一般 tableview 中有多个问题的答案怎么办?如果如果有一个问题和一些答案 我同意一个表格视图可以解决我的问题,但如果我有超过 1 个问题怎么办?
  • setTableViewDataSourceDelegate 在您当前的代码中没有被调用

标签: ios swift uitableview


【解决方案1】:

我建议你只使用一个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
    }
}

【讨论】:

  • 所以我们将在您的示例中使用两个单元格?我不明白我必须如何在情节提要中附加这些单元
  • 您可以在没有表格视图的情况下使用您现在拥有的内容更改单元格,每个单元格中只有一个标签
  • 一般来说,我需要一些单元格作为表格视图行,但我不明白如何将它分配给情节提要,它的呈现位置
  • @Andrew 您需要在 tableView 中使用两个原型单元格一个用于标题问题,其他用于回答
【解决方案2】:

试试这个

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
        }
    }

【讨论】:

    猜你喜欢
    • 2019-04-18
    • 2021-06-05
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    相关资源
    最近更新 更多