2016-05-31 31 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,否則它不會轉到另一個視圖。請記住刪除故事板中的觸摸動作 –