2015-08-16 79 views
-2

好吧,我阅读了很多关于它的文章,但我想为此提供一个建议。所以我tableViewCell看起来是这样的:如何使用Swift将数据从TableViewCell传递到ViewController

enter image description here

如果在名字Akshay Kheveria用户点击它会执行SEGUE到一个视图控制器和显示的Akshay Kheveria轮廓。所以我在想什么只是通过用户的objectId和加载所需的数据解析在另一个viewController..so:

  1. 我该怎么做?
  2. 这是做这件事的正确方法还是有其他事情要了解?

谢谢你的时间。

+0

你有用户唯一对象ID的用户数据,同时转变? – rishi

+0

这里有关于如何在目标vc上分配属性的很好的答案,但它们都建议分配给id属性。如果目标vc需要一个PFObject(可能),并且你有一个(可能)然后分配PFObject。 – danh

回答

1

关键是要用(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender的方法。 开始在你的ViewController创建一个公共属性(头文件):

@property (nonatomic) NSNumber *profileId; 

保存由用户选择,当他摸到假,像这样的例子小区:

- (void)tableView:(UITableView *)tableView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 

    [self performSegueWithIdentifier:@"ToProfile" sender:indexPath]; 
} 

然后

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    NSIndexPath* indexPath = (NSIndexPath *)sender; 
    SomeObject* tableViewItem = [self.data objectAtIndex:indexPath.row]; 
    if ([[segue identifier] isEqualToString:@"ToProfile"]) { 
     ProfileViewController *profile = [segue destinationViewController]; 

     profile.profileId = tableViewItem.profileId; 
    } 
} 

所以现在你可以从你的ViewController访问profileId属性。

+0

当通过单元格选择触发segue时,sender参数就是所选单元格本身。因此,不需要为所选单元格存储属性,并且可以使用'[self.tableView indexPathForCell:sender]'检索'prepareForSegue:sender:'方法中的索引。 – andreamazz

+0

噢,真好!我不知道。谢谢 – Max

+0

你可以在Swift中解释一下吗? –

3
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "ToProfile" 
    { 
    if let destinationVC = segue.destinationViewController as? ProfileViewController{ 
     destinationVC.profileId = yourObjectId 
    } 
    } 
} 

试试这个..

相关问题