如何使用Swift将数据从TableViewCell传递到ViewController
好吧,我阅读了很多关于它的文章,但我想为此提供一个建议。所以我tableViewCell
看起来是这样的:如何使用Swift将数据从TableViewCell传递到ViewController
如果在名字Akshay Kheveria
用户点击它会执行SEGUE到一个视图控制器和显示的Akshay Kheveria
轮廓。所以我在想什么只是通过用户的objectId和加载所需的数据解析在另一个viewController..so:
- 我该怎么做?
- 这是做这件事的正确方法还是有其他事情要了解?
谢谢你的时间。
关键是要用(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属性。
当通过单元格选择触发segue时,sender参数就是所选单元格本身。因此,不需要为所选单元格存储属性,并且可以使用'[self.tableView indexPathForCell:sender]'检索'prepareForSegue:sender:'方法中的索引。 – andreamazz
噢,真好!我不知道。谢谢 – Max
你可以在Swift中解释一下吗? –
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ToProfile"
{
if let destinationVC = segue.destinationViewController as? ProfileViewController{
destinationVC.profileId = yourObjectId
}
}
}
试试这个..
你有用户唯一对象ID的用户数据,同时转变? – rishi
这里有关于如何在目标vc上分配属性的很好的答案,但它们都建议分配给id属性。如果目标vc需要一个PFObject(可能),并且你有一个(可能)然后分配PFObject。 – danh