下面的代码不会导致注销按钮工作,这是怎么回事?



下面的代码没有导致注销按钮工作,怎么了?!?

import UIKit
import FirebaseAuth
class ProfileViewController: UIViewController {

// Set Log Out Button
@IBOutlet var tableView: UITableView!

// Data option
let data = ["Log Out"]

// Supply Data Source and Delegate Self
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.delegate = self
tableView.dataSource = self
}
}
// Add an externsion to inform Data Source and Delegate
extension ProfileViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}

// Deque row and style text
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = data[indexPath.row]
cell.textLabel?.textAlignment = .center
cell.textLabel?.textColor = .red
return cell
}

// Make logout to occur when user log outs - unhighlight cell
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)


// Create an alert to Log Out
let actionsheet = UIAlertController(title: "",
message: "",
preferredStyle: .actionSheet)

actionsheet.addAction(UIAlertAction(title: "Log Out",
style: .destructive,
handler: { [weak self] _ in

guard let strongSelf = self else {
return
}

do {
try FirebaseAuth.Auth.auth().signOut()
let vc = LogInViewController()
let nav = UINavigationController(rootViewController: vc)
nav.modalPresentationStyle = .fullScreen
strongSelf.present(nav, animated: true)
}
catch {
print("Failed to log out")
}
}))

actionsheet.addAction(UIAlertAction(title: "Cancel",
style: .cancel,
handler: nil))

present(actionsheet, animated: true)
}
}

您似乎使用了didDeselectRowAt而不是didSelectRowAtdidSelectRowAt是当用户点击单元格并选择它时调用的内容,didDeselectRowAt是当取消选择单元格时调用的。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let actionsheet = UIAlertController(title: "",
message: "",
preferredStyle: .actionSheet)
actionsheet.addAction(UIAlertAction(title: "Log Out",
style: .destructive,
handler: { [weak self] _ in
guard let strongSelf = self else {
return
}
do {
try FirebaseAuth.Auth.auth().signOut()
let vc = LogInViewController()
let nav = UINavigationController(rootViewController: vc)
nav.modalPresentationStyle = .fullScreen
strongSelf.present(nav, animated: true)
}
catch {
print("Failed to log out")
}
}))
actionsheet.addAction(UIAlertAction(title: "Cancel",
style: .cancel,
handler: nil))
present(actionsheet, animated: true)
}

最新更新