我有一个有两种状态的UITableView。一个是用户登录而另一个是用户注销的。两者都显示不同的细节如何设置tableview,以便当用户在tableview中选择某些内容时,它会转到指定的视图控制器。感谢。
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)
}
答案 0 :(得分:1)
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Row #\((indexPath as NSIndexPath).row)")
let cell = tableView.cellForRow(at: indexPath) as! popTableViewCell
if Auth.auth().currentUser != nil {
if cell.textLabel?.text! == "Find Event"{
print(loggedIn[(indexPath as NSIndexPath).row])
performSegue(withIdentifier: "menutofind", sender: nil)
}
else if cell.textLabel?.text! == "Create Event"{
print(loggedIn[(indexPath as NSIndexPath).row])
performSegue(withIdentifier: "menutocreate", sender: nil)
}
else if cell.textLabel?.text! == "Past Events" {
print(loggedIn[(indexPath as NSIndexPath).row])
performSegue(withIdentifier: "menutopast", sender: nil)
}
else if cell.textLabel?.text! == "Recently Viewed" {
print(loggedIn[(indexPath as NSIndexPath).row])
performSegue(withIdentifier: "menutorv", sender: nil)
}
else if cell.textLabel?.text! == "Help" {
print(loggedIn[(indexPath as NSIndexPath).row])
performSegue(withIdentifier: "menutohelp", sender: nil)
}
else if cell.textLabel?.text! == "Setting" {
print(loggedIn[(indexPath as NSIndexPath).row])
performSegue(withIdentifier: "menutosetting", sender: nil)
}
}
else {
if cell.textLabel?.text! == "Log In" {
performSegue(withIdentifier: "menutologin", sender: nil)
}
else if cell.textLabel?.text! == "Find Event" {
performSegue(withIdentifier: "menutofind", sender: nil)
}
else if cell.textLabel?.text! == "Create Event" {
performSegue(withIdentifier: "menutocreate", sender: nil)
}
else if cell.textLabel?.text! == "Help" {
performSegue(withIdentifier: "menutohelp", sender: nil)
}
}
}
通过这个你可以对你想要的VC进行Segue。
希望它有所帮助。
答案 1 :(得分:1)
为登录和注销状态创建两个所需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)
}
}