我有一个有两种状态的UITableView。一个是用户登录而另一个是用户注销的。两者都显示不同的细节,每个细节填充大约4行。我现在拥有的东西不起作用。我该如何解决它,或者我应该用更好的代码替换它。感谢
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if Auth.auth().currentUser != nil {
if loggedIn.contains("Find Event"){
performSegue(withIdentifier: "menutofind", sender: nil)
}
else if loggedIn.contains("Create Event"){
performSegue(withIdentifier: "menutocreate", sender: nil)
}
else if loggedIn.contains("Past Events"){
performSegue(withIdentifier: "menutopast", sender: nil)
}
else if loggedIn.contains("Recently Viewed"){
performSegue(withIdentifier: "menutorv", sender: nil)
}
else if loggedIn.contains("Help"){
performSegue(withIdentifier: "menutohelp", sender: nil)
}
else if loggedIn.contains("Setting"){
performSegue(withIdentifier: "menutosetting", sender: nil)
}
}
else {
if loggedOut.contains("Log In"){
performSegue(withIdentifier: "menutologin", sender: nil)
}
else if loggedOut.contains("Find Event"){
performSegue(withIdentifier: "menutofind", sender: nil)
}
else if loggedOut.contains("Create Event"){
performSegue(withIdentifier: "menutocreate", sender: nil)
}
else if loggedOut.contains("Help"){
performSegue(withIdentifier: "menutohelp", sender: nil)
}
}
}
答案 0 :(得分:2)
因此,如果我正确理解了这一点,那么您希望根据您点击的单元格触发特定的segue。您当前的代码将不起作用,因为您的第一个if
语句将始终解析为true,这可能是您只触发第一个segue的原因。
更好的方法是使用当前单元格的indexPath.row
并根据它选择一个segue,这将大大减少您的代码。我还建议您创建一个struct
,这样您就可以存储更多数据并在tableView
中更有效地使用它。
我在下面的代码中做了一些更改,看看是否有帮助。
struct UserCell {
public let title: String
public let segue: String
init(title: String, segue: String) {
self.title = title
self.segue = segue
}
}
class SideTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var loggout: UIButton!
@IBOutlet weak var tableview: UITableView!
@IBOutlet weak var dismissButton: UIButton!
private let loggedIn: [UserCell] = [
UserCell(title: "Find Event", segue: "menutofind"),
UserCell(title: "Create Event", segue: "menutocreate"),
UserCell(title: "Past Events", segue: "menutopast"),
UserCell(title: "Recently Viewed", segue: "menutorv"),
UserCell(title: "Help", segue: "menutohelp"),
UserCell(title: "Setting", segue: "menutosetting")
]
private let loggedOut: [UserCell] = [
UserCell(title: "Log In", segue: "menutologin"),
UserCell(title: "Find Event", segue: "menutofind"),
UserCell(title: "Create Event", segue: "menutocreate"),
UserCell(title: "Help", segue: "menutohelp")
]
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].title
return cell
} else {
// No user is signed in.
cell.textLabel?.text = loggedOut[indexPath.item].title
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if Auth.auth().currentUser != nil {
performSegue(withIdentifier: self.loggedIn[indexPath.row].segue, sender: nil)
}
else {
performSegue(withIdentifier: self.loggedOut[indexPath.row].segue, sender: nil)
}
}
UserCell
结构可以轻松扩展,以存储更多数据,并在您需要时轻松访问它。
答案 1 :(得分:1)
我解决了同样的问题,在我的情况下,我使用了两个数组来存储选项,因此您将存储将在用户登录时显示的选项,另一个您将存储将出现的选项,如果用户是退出。
然后我使用 indexPath.row 来检测选择了哪个位置。不要忘记使用 if 来检测用户是否已登录。
答案 2 :(得分:0)
这是完整的代码
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) {
if Auth.auth().currentUser != nil {
if loggedIn.contains("Find Event"){
performSegue(withIdentifier: "menutofind", sender: nil)
}
else if loggedIn.contains("Create Event"){
performSegue(withIdentifier: "menutocreate", sender: nil)
}
else if loggedIn.contains("Past Events"){
performSegue(withIdentifier: "menutopast", sender: nil)
}
else if loggedIn.contains("Recently Viewed"){
performSegue(withIdentifier: "menutorv", sender: nil)
}
else if loggedIn.contains("Help"){
performSegue(withIdentifier: "menutohelp", sender: nil)
}
else if loggedIn.contains("Setting"){
performSegue(withIdentifier: "menutosetting", sender: nil)
}
}
else {
if loggedOut.contains("Log In"){
performSegue(withIdentifier: "menutologin", sender: nil)
}
else if loggedOut.contains("Find Event"){
performSegue(withIdentifier: "menutofind", sender: nil)
}
else if loggedOut.contains("Create Event"){
performSegue(withIdentifier: "menutocreate", sender: nil)
}
else if loggedOut.contains("Help"){
performSegue(withIdentifier: "menutohelp", sender: nil)
}
}
}