gpt4 book ai didi

ios - 在 UITableView 上执行具有多个状态的 segue 的问题

转载 作者:行者123 更新时间:2023-11-28 12:17:57 24 4
gpt4 key购买 nike

我有一个具有两种状态的 UITableView。一种是用户登录时,另一种是用户注销时。两者都显示不同的细节。我如何设置 TableView ,以便当用户在 TableView 中选择某些内容时,它会转到指定的 View Controller 。谢谢。

import UIKit
import Firebase

class SideTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {



@IBOutlet weak var loggout: UIButton!
@IBOutlet weak var tableview: UITableView!
@IBOutlet weak var dismissButton: UIButton!

var loggedIn = ["Find Event", "Create Event", "Past Events", "Recently Viewed", "Help", "Setting"]

var loggedOut = ["Log In", "Find Event", "Create Event", "Help"]


override func viewDidLoad() {
super.viewDidLoad()
dismissButton.layer.cornerRadius =
dismissButton.frame.size.width / 2
loggout.layer.cornerRadius =
loggout.frame.size.width / 2
if Auth.auth().currentUser != nil {
loggout.isHidden = false
print("User Signed In, Showing Relivent Info")

} else {
loggout.isHidden = true
print("User Not Logged In, Showing Relivent Info")
}
tableview.reloadData()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if Auth.auth().currentUser != nil {
// User is signed in.
return loggedIn.count

} else {
// No user is signed in.
return loggedOut.count
}
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customcell", for: indexPath as IndexPath)
if Auth.auth().currentUser != nil {
// User is signed in.
cell.textLabel?.text = loggedIn[indexPath.item]
return cell

} else {
// No user is signed in.
cell.textLabel?.text = loggedOut[indexPath.item]
return cell
}
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//performSegue(withIdentifier: "tologinfrommain", sender: nil)
}

最佳答案

像这样为登录和注销状态创建两个所需 segue 的静态数组。

@IBOutlet weak var loggout: UIButton!
@IBOutlet weak var tableview: UITableView!
@IBOutlet weak var dismissButton: UIButton!

var loggedIn = ["Find Event", "Create Event", "Past Events", "Recently Viewed", "Help", "Setting"]

var loggedOut = ["Log In", "Find Event", "Create Event", "Help"]

satatic var loggedInSegues = ["FindEventSegue", "CreateEventSegue", "PastEventSegue", "RecentlyViewedSegue", "HelpSegue", "SettingsSegue"]

static var loggedOutSegues = ["LogInSegue", "FindEventSegue", "CreateEventSegue", "HelpSegue"]

override func viewDidLoad() {
super.viewDidLoad()
dismissButton.layer.cornerRadius =
dismissButton.frame.size.width / 2
loggout.layer.cornerRadius =
loggout.frame.size.width / 2
if Auth.auth().currentUser != nil {
loggout.isHidden = false
print("User Signed In, Showing Relivent Info")

} else {
loggout.isHidden = true
print("User Not Logged In, Showing Relivent Info")
}
tableview.reloadData()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if Auth.auth().currentUser != nil {
// User is signed in.
return loggedIn.count

} else {
// No user is signed in.
return loggedOut.count
}
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customcell", for: indexPath as IndexPath)
if Auth.auth().currentUser != nil {
// User is signed in.
cell.textLabel?.text = loggedIn[indexPath.item]
return cell

} else {
// No user is signed in.
cell.textLabel?.text = loggedOut[indexPath.item]
return cell
}
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if Auth.auth().currentUser != nil {
performSegue(withIdentifier:loggedInSegues[indexPath.row], sender: nil)
} else {
performSegue(withIdentifier: loggedOutSegues[indexPath.row], sender: nil)
}
}

关于ios - 在 UITableView 上执行具有多个状态的 segue 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45831090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com