【发布时间】:2020-02-20 21:49:25
【问题描述】:
所以我有 2 个视图显示在 UIView 中,这取决于在 SegmentViewController 上选择的内容。我创建虚拟数据,返回 20 行自定义单元格。这很好用。
一切都很好,直到我与 TableView 交互。
下面是我的代码:
GoalsViewController.swift
import UIKit
class GoalsViewController: UIViewController {
@IBOutlet weak var goalsTableView: UITableView!
let goalCellIdentifier = "goalCell"
override func viewDidLoad() {
super.viewDidLoad()
goalsTableView.delegate = self
goalsTableView.dataSource = self
goalsTableView.register(UINib(nibName: "GoalsViewCell", bundle: nil), forCellReuseIdentifier: goalCellIdentifier)
goalsTableView.reloadData()
}
}
extension GoalsViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = goalsTableView.dequeueReusableCell(withIdentifier: goalCellIdentifier, for: indexPath) as! GoalsViewCell
cell.goalTitle.text = "aaaaa"
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Selected \(indexPath.row)")
}
}
选择任何空行后,不会调用didSelectRowAt,因此单元格根本不存在。我试图找到一个解决方案,但我只是在被填充之前找到关于空列表的问题。
空的tableview可能是什么原因?
【问题讨论】:
-
你在其他地方打电话给
reloadData吗? -
看起来您包含的视图没有正确约束,因此一旦有自动布局通道,它们就会被调整大小或移出屏幕。您是否在控制台中收到约束警告?为什么显示“调试视图层次结构”?尝试“揭示”。这是一个很棒的工具
-
@dfd 不,我不会在其他地方称呼它。甚至不在主 ViewController 上。 @Paulw11 这是一个很棒的工具。谢谢你的建议,不知道!因此,我将文本名称更改为
indexPath.row以了解哪一行。当列表已满时,我可以用白色看到可见的项目。单击后,所有内容都变为灰色,但值仍然存在。这是截图i.imgur.com/OjDYjqY.png
标签: ios swift xcode uitableview