尝试从表视图传递多个值到另一个视图控制器
问题描述:
我想传递整个用户信息,我有一个用户类。我想将用户名,名称,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!)
}
答
子类的UITableViewCell(创建类型的UITableViewCell的新文件,并调用它像UserTableViewCell添加类型用户的属性(或任何你的用户类)。在didSelectRowAtIndexPath方法设置的UIViewController相同的用户属性到UserTableViewCell的User属性,然后呈现视图控制器
提示:'prepareForSegue()'。在目标视图控制器中设置变量/数组以接收此数据并在当前视图控制器中为此方法分配值。祝你好运! –
我会推荐 - 起初至少 - 使用'struct'来处理TableView中的数据。因此,引用另一个ViewCo中的数据ntroller与创建该类型的全局对象一样简单。 – KSigWyatt
你也可以在目标类中创建一个元组,并将元组从一个类传递到具有所有值的另一个类 –