【发布时间】:2020-07-29 07:14:26
【问题描述】:
我想显示带有单元格的表格视图,其中将是一个标签和该标签下方的表格视图。我现在有这样的布局:
在某些情况下,我的表格视图上方的视图将被隐藏,因此我将在整个屏幕上显示表格视图。所以...我找到了this video,开发人员设法解决了我的任务。我所做的一切都与他的视频类似,但我没有设法在表格视图单元格中显示表格视图。这是我的步骤:
- 将所有视图添加到常规视图。
- 在我的表格视图中附加了标签:主表格视图为 100,内部表格视图为 90。
- 创建单元类并将它们附加到两个单元。
- 在主单元格中添加了扩展,如视频中。
- 在主视图控制器中处理表视图标签。
正如我所见,问题在于无法显示的内部表格视图。您可以在下面看到单元格的类。主细胞:
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