2017-11-18 260 views
-2

我想傳遞整個用戶信息,我有一個用戶類。我想將用戶名,名稱,uid,圖像傳遞給另一個視圖控制器。嘗試從表視圖傳遞多個值到另一個視圖控制器

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! NewChatTableViewCell 

     let user = usersarray[indexPath.row] 
     let ColorView = UIView() 
     ColorView.backgroundColor = UIColor(red: 0.05, green: 0.15, blue: 0.28, alpha: 0.7) 
     cell.selectedBackgroundView = ColorView 
     cell.backgroundColor = UIColor.clear 
     cell.backgroundView?.backgroundColor = UIColor.clear 
     cell.firstname.text = user.firstname! + " " + user.lastname! 
     cell.username.text = "@" + user.username! 
     cell.userimage?.layer.masksToBounds = true 
     cell.userimage.layer.cornerRadius = 20 
     cell.userimage.clipsToBounds = true 
     if let profileimage = user.profilepic 
     {cell.userimage.loadImageCache(urlString: profileimage)} 
     return cell 
    } 

    func callmessage(){ 
     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     let controller = storyboard.instantiateViewController(withIdentifier: "message") 
     self.present(controller, animated: true, completion: nil) 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     let indexPath = tableView.indexPathForSelectedRow! 
     callmessage(user: User!) 
    } 
+0

提示:'prepareForSegue()'。在目標視圖控制器中設置變量/數組以接收此數據並在當前視圖控制器中爲此方法分配值。祝你好運! –

+0

我會推薦 - 起初至少 - 使用'struct'來處理TableView中的數據。因此,引用另一個ViewCo中的數據ntroller與創建該類型的全局對象一樣簡單。 – KSigWyatt

+0

你也可以在目標類中創建一個元組,並將元組從一個類傳遞到具有所有值的另一個類 –

回答

0

子類的UITableViewCell(創建類型的UITableViewCell的新文件,並調用它像UserTableViewCell添加類型用戶的屬性(或任何你的用戶類)。在didSelectRowAtIndexPath方法設置的UIViewController相同的用戶屬性到UserTableViewCell的User屬性,然後呈現視圖控制器

相關問題