[英]Table view cells not loading in view controller
Swift正如你在这个Swift代码中看到的,我正在尝试为我的注销控制器创建一个表视图。 但是,我遇到了“注销”按钮在视图控制器中显示为单元格的问题。 我不知道问题是什么,因为它的构建没有任何错误,我已经去检查了几次但找不到问题。
import UIKit
struct SettingsCellModel {
let title: String
let handler: (() -> Void)
}
final class SettingsViewController: UIViewController {
private var tableView: UITableView {
let tableView = UITableView(frame: .zero, style: .grouped)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
return tableView
}
private var data = [[SettingsCellModel]]()
override func viewDidLoad() {
super.viewDidLoad()
configureModels()
view.backgroundColor = .systemBackground
view.addSubview(tableView)
tableView.delegate = self
tableView.dataSource = self
// Do any additional setup after loading the view.
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.frame = view.bounds
}
private func configureModels() {
let section = [SettingsCellModel(title: "Log Out") { [weak self] in
self?.didTapLogOutButton()
}
]
data.append(section)
}
private func didTapLogOutButton() {
let actionSheet = UIAlertController(title: "Log Out", message: "Are you sure you want to log out", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
actionSheet.addAction(UIAlertAction(title: "Log Out", style: .destructive, handler: { _ in
logOut (completion: { success in
DispatchQueue.main.async {
if success {
//present log in
let loginVC = LoginViewController()
loginVC.modalPresentationStyle = .fullScreen
self.present(loginVC, animated: true) {
self.navigationController?.popToRootViewController(animated: false)
self.tabBarController?.selectedIndex = 0
}
}
else {
//error occurred
fatalError("Could not log out user")
}
}
})
}))
actionSheet.popoverPresentationController?.sourceView = tableView
actionSheet.popoverPresentationController?.sourceRect = tableView.bounds
present(actionSheet, animated: true)
}
}
extension SettingsViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = data[indexPath.section][indexPath.row].title
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
data[indexPath.section][indexPath.row].handler()
}
}
问题是您正在使用 Computed property private var tableView: UITableView {
这意味着每次您在代码中访问tableView
,它的闭包都会被执行(或它的值被评估),并且因为您在其闭包中实例化了一个新的 tableView 实例,您会收到不同的所有 3 个语句中的 tableView 实例
view.addSubview(tableView)
tableView.delegate = self
tableView.dataSource = self
因此,您添加为 subView 的 tableView 实例与将委托和数据源设置为 self 的实例不同。
你需要什么
选项1:
private var tableView: UITableView = {
let tableView = UITableView(frame: .zero, style: .grouped)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
return tableView
}()
选项 2:(首选)
private lazy var tableView: UITableView = {
let tableView = UITableView(frame: .zero, style: .grouped)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
return tableView
}()
希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.