2016-05-31 49 views
0

我想允许用户选择一行,并给定该朋友的关系,在点击行时发生不同的操作。例如,如果朋友,然后聊天打开,如果不是朋友,则可以允许发送朋友请求,等等。允许对tableview行进行条件操作select

目前,我已经实现了以下,但在每一行的点击,无论朋友的状态与否,聊天打开并抛出一个错误所产生的IndividualChatControllerfriendChat对象:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    tableView.deselectRowAtIndexPath(indexPath, animated: true) 

     // Ensure controller knows which dataset to pull from, 
     // so detail view is correct 
     let friendChat: Friend 
     if searchController.active && searchController.searchBar.text != "" { 
      friendChat = filterMappedFriends[indexPath.row] 
     } else { 
      friendChat = mappedFriends[indexPath.row] 
     } 

     // Now set the conditional cases: if a friend then chat, if user then friend request if not user then can invite them: 
     if(friendChat.statusSort == 2) { 
      performSegueWithIdentifier("showIndividualChat", sender: self) 

      func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

       if segue.identifier == "showIndividualChat" { 

        let controller = segue.destinationViewController as! IndividualChatController 
         controller.friendChat = friendChat 
         controller.senderId = FeastGlobal.sharedInstance.userID 
         controller.senderDisplayName = FeastGlobal.sharedInstance.userName 
       } 
      } 
     } else if (friendChat.statusSort == 1) { 

      print("Can invite to be friend") 

     } else if (friendChat.statusSort == 0) { 

      print("Invite to Feast") 

     } 

} 

主营:

enter image description here

近拍的SEGUE名称:

enter image description here

回答

0

你确定它不适用于其他statusSort情况吗?其他的看起来不错,但是statusSort == 2不应该工作,因为你将prepareForSegue定义为一个新的嵌套函数。我想知道,如果你只是看到statusSort == 2的问题,并将其误认为其他人?

原因statusSort == 2将不起作用是因为您重新定义prepareForSegue作为条件内的嵌套函数,它需要作为覆盖视图控制器的函数。将它与override func tableView放在同一级别作为另一个重写。

现在它不会被调用,所以你正在为statusSort == 2执行segue而不设置目标视图控制器的部分。这就是为什么它抱怨friendChat,它没有设置。

像这样:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    // You're already doing this part. 
} 

// Move your prepareForSegue out here like this: 
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    // Do your stuff here with the destination view controller. You have multiple options here to handle the upcoming segue properly. 
    // Option 1: Use the segue identifier from your existing code so you can distinguish between multiple segues here. 
    // <YOUR CODE HERE> 
    // Option 2: If you segue to distinct view controllers, you can just check for the view controller type here. Either do this through optional downcast or use the "is" operator and then cast inside the conditional blocks. The former is shown below and is cleaner. 
    if let someViewController: SomeViewController = segue.destinationViewController as? SomeViewController { 
     // This is a segue headed to SomeViewController. Now we can set SomeViewController here. I'm just making up a bar here. 
     let foo: Int = 1 
     someViewController.bar = foo 
    } else if let anotherViewController: AnotherViewController = segue.destinationViewController as? AnotherViewController { 
     // Another segue headed to AnotherViewController. 
     let foo: Int = 1 
     anotherViewController.bar = foo 
    } 
} 
+0

如果我执行你的代码,其他操作假定使用'Segue'的,再扔错误。你的回答是不完整的 – Sauron

+1

这个答案是正确的。当你调用'performSegueWithIdentifier'时,你应该传递'friendChat'作为'sender',这样你就可以在'prepareForSegue'中访问它。确保你没有从表格视图单元的'action'到故事板中的下一个视图控制器的链接 – Paulw11

+0

我在那里发表评论,告诉你把你的东西放在那里。我的答案是完整的,无论您是否希望我在其中复制和粘贴其余代码。 编辑:这里让我走得更远。 – Jay

0

你不应该把prepareForSegue方法在didSelectRow方法。
这样做会导致prepareForSegueMethod未被调用。

这是错误的:

class YourClass: UITableViewController { 

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     // Don't put in here 
     func prepareForSegue(sgue: UIStoryboardSegue, sender: AnyObject?) { 
      ... 
     } 
    } 

} 

相反,把它直接在类中:

class YourClass: UITableViewController { 

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     ... 
    } 

    // Put in here 
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     ...  
    } 
} 
+0

如何处理不应该转到另一个视图但只触发服务器操作的行的选择? – Sauron

+0

即。我不希望所有行都移动到新视图。根据他们的数据,他们不应该在用户选择时做任何事情。这是如何实现的? – Sauron

+1

只检查didSelectRowAtIndexPathMethod内的数据。除非您调用performSegueWithIdentifier,否则它不会转到另一个视图。请记住删除故事板中的触摸动作 –