简体   繁体   中英

Issue to choose a cell in a section of a TableView

I have a UITableViewController with three sections and a variable number of cells for each one and when I tap on a cell, I have a detail view.

I use a segue to send the information to my other view. But I saw that if I the for example the first cell of the second section I will have the first cell of my first section. I tried to fix that by indicating the section and the cell but my program doesn't recognize indexPath.section.

Here is my code of the segue :

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

if ([segue isKindOfClass:[TLAnimatedSegue class]]) {
        ((TLAnimatedSegue *) segue).delegate = self;
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        TVDetailsViewController *destViewController = segue.destinationViewController;
        destViewController.details = [[_scheduleList objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
    }
}

I don't understand why I have en error with that because when I po my indexPath, I have two element inside. With the breakpoint, this line seems to be the reason of the crash :

destViewController.details = [[_scheduleList objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];

Thanks for your help.

Thank you @zcui93 and @Rohit KP, you open my mind and eyes so here is the solution to tackle this little issue and now it works :

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue isKindOfClass:[TLAnimatedSegue class]]) {
        ((TLAnimatedSegue *) segue).delegate = self;
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        TVDetailsViewController *destViewController = segue.destinationViewController;
        if(indexPath.section == 0) {
            destViewController.details = [_pastScheduleList objectAtIndex:indexPath.row];
        }

        if(indexPath.section == 1) {
        destViewController.details = [_currentScheduleList objectAtIndex:indexPath.row];
        }

        if(indexPath.section == 2) {
            destViewController.details = [_futurScheduleList objectAtIndex:indexPath.row];
        }

    }
}

Use didSelectRowAtIndexPath method and get cell content using as describe below and perform segue

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
....
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
String id = cell.textLabel.text;
/*... perform segue operation... */
....
}


/* ...  for( Customcell *cell = (CustomCell*)... ) ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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