简体   繁体   English

IOS / Objective-C:传递数据(数组)并接收详细的字符串

[英]IOS/Objective-C: Passing data (array) and receiving detailed strings

I'm trying to pass data (array) with PrepareForSegue into a detailed VC. 我正在尝试将PrepareForSegue的数据(数组)传递到详细的VC中。 The destination VC has a label that receives a string. 目标VC具有接收字符串的标签。 I thought I could transform the array into a string, and the rest was straightforward. 我以为可以将数组转换成字符串,其余的很简单。 This was achieved, though, but kind of wrongly, because, obviously, the text I get, regardless the row I click, returns me the whole object...And my goal is to get details of a specific row...Can someone help me? 尽管实现了这一点,但是有点不对劲,因为很明显,无论点击哪个行,我得到的文本都会返回整个对象...我的目标是获取特定行的详细信息...有人帮我? Guess this is basic stuff, but this is my first app...The method PrepareForSegue: 猜猜这是基本的东西,但这是我的第一个应用程序... PrepareForSegue方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"detail"]) {

        NSString *places = [_spots componentsJoinedByString:@" "];

        DetailViewController * destination = segue.destinationViewController;
        destination.detailedText =  places;
}

EDIT: 编辑:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSManagedObject *ls = [self.spots objectAtIndex:indexPath.row];
    NSLog(@"Table Spots are: %@", ls);
    [cell.textLabel setText:[NSString stringWithFormat:@"%@", [ls valueForKey:@"name"]]];
    cell.imageView.image = [UIImage imageNamed:@"city.jpg"];
    return cell;
}

As far as I understood, you want to performSegue() on a row selection and you want to pass a data specific to the selected row. 据我了解,您希望对行选择执行performSegue() ,并且希望传递特定于所选行的数据。 In order to do this you can use the tableView.indexPathForSelectedRows() method, which is going to return an array with the indexPaths of all selected rows. 为此,您可以使用tableView.indexPathForSelectedRows()方法,该方法将返回一个包含所有选定行的indexPaths的数组。 Then you can get the UITableViewCells for this indexPaths and get their data. 然后,您可以获取此indexPathsUITableViewCells并获取其数据。

You can pass the string value to the perforSegue method as the sender: 您可以将字符串值作为发送者传递给perforSegue方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"detail" sender:[_spots objectAtIndex:indexPath.row]];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"detail"]) {
        DetailViewController * destination = segue.destinationViewController;
        destination.detailedText = sender;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM