سؤال

I have a UITableView showing data from a plist file. For this table view, I've simply made an NSArray from the contents of the plist file and fed the data into each cell. But when a cell is tapped, I need the next table view controller to know the index path of the cell that was tapped so that this view controller knows which item in the array to show details for. I've tried the following method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    staffDetailView = [[StaffDetailViewController alloc] init];
    [staffDetailView setSelectedIndexPath:indexPath];

    [self performSegueWithIdentifier:@"pushStaffDetailView" sender:self];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Above, I'm attempting to pass the index path of the tapped cell to an NSIndexPath object that I've created in the new view controller.

The next table view controller's header file:

@interface StaffDetailViewController : UITableViewController {

    NSIndexPath *selectedIndexPath;
    NSArray *staffArray;

}

@property (nonatomic, retain) NSIndexPath *selectedIndexPath;
@property (nonatomic, strong) NSArray *staffArray;

Here I define the selectedIndexPath object that was set in the previous view controller.

But when I try to access the selectedIndexPath object to display the relevant data, the object appears to be nil:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *header;
    if (section == 0) {
        NSString *name = [[staffArray objectAtIndex:selectedIndexPath.row] objectForKey:@"Staff Member"];
        NSArray *splitStaffArray = [name componentsSeparatedByString:@", "];
        NSString *lastName = [splitStaffArray objectAtIndex:0];
        NSString *firstName = [splitStaffArray objectAtIndex:1];
        NSString *consolidatedName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
        NSString *assignment = [[staffArray objectAtIndex:selectedIndexPath.row] objectForKey:@"Assignment"];
        header = [NSString stringWithFormat:@"%@\n%@", consolidatedName, assignment];
        return header;
    }
    return nil;
}

Am I accessing the selectedIndexPath object incorrectly? Is there a superior method for passing the correct index path to the next view controller?

هل كانت مفيدة؟

المحلول

I revised the first table view controller's didSelectRowAtIndexPath method:

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

    [self performSegueWithIdentifier:@"pushStaffDetailView" sender:self];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    [staffDetailView setSelectedIndexPath:indexPath];
    [staffDetailView setPassedName:[[staffArray objectAtIndex:indexPath.row] objectForKey:@"Staff Member"]];   

}

It seemed that for some reason I needed to perform the segue before accessing that view controller's instance variables. Strange that I couldn't just alloc init the view controller its self. Anyhow, the index path is now being correctly pushed across view controllers.

نصائح أخرى

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //I ll use NSUserDefaults to store tha indexpath. Indexpath.row is an int property, so convert to NSNumber 

    NSNumber *ip = [NSNumber numberWithInt:indexpath.row];
    NSUserDefaults *saveIP=[NSUserDefaults standartuserdefaults];
    [saveIP setObject: ip foKey:@"theIndexPath"];

    [self performSegueWithIdentifier:@"pushStaffDetailView" sender:self];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    [staffDetailView setSelectedIndexPath:indexPath];
    [staffDetailView setPassedName:[[staffArray objectAtIndex:indexPath.row] objectForKey:@"Staff Member"]];   

}

I other view controller retrieve saved object(data) with the code below

NSNumber *theIndexpath= [[NSUserDefaults standartuserdefaults] objectForKey:@"theIndexPath"];

Note: NSUserDefaults may not be a good idea but it was very easy, and still works for my app

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top