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

Posted

技术标签:

【中文标题】在 UITableView 上执行具有多个状态的 segue 的问题【英文标题】:problems with perform segue with multiple states on a UITableView 【发布时间】:2017-08-23 05:13:27 【问题描述】:

我有一个 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)
        

【问题讨论】:

您需要在didSelectRowAt中执行适当的segue,而不是使用情节提要中的动作连接器触发segue 【参考方案1】:

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

@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)
            
        

【讨论】:

【参考方案2】:
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。

希望对你有帮助。

【讨论】:

当我选择一些东西时,它总是会把我带到第一个 performSegue。我该如何解决? 嗨@JameelMaqbool 如果它不是您使用的,您只需删除该行。我也更新了答案。请看一看。 @JameelMaqbool 有效吗? 请看我下面的回答,看看我有什么代码。我仍然不明白为什么它不起作用。谢谢 @JameelMaqbool 请看一下,因为我更新了经过测试的 Ans 希望它会起作用。根据您的需要

以上是关于在 UITableView 上执行具有多个状态的 segue 的问题的主要内容,如果未能解决你的问题,请参考以下文章

具有多个 UITableView(s) 的相同原型单元格

具有多个 NSFetchedResultsControllers 的 UITableView 导致断言失败

具有多个部分的静态分组 UITableView 的默认外观

UITableView 具有多个具有多行的部分。我需要部分独立运行

具有多个 UIScrollView 类和/或子类的 setScrollsToTop (UITableView)

如何打印具有多个数据的标题和uitableview的uiwebview