Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
799 views
in Technique[技术] by (71.8m points)

objective c - Pass Index Number between UITableView List segue

When I click a certain list item in my UITableView, I want to pass the index of the item that I clicked on to the next detailed view.

This is my relevant code so far:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeworkDetails *view =  [[HomeworkDetails alloc] init];


    int lastIndex = [indexPath indexAtPosition:[indexPath length] - 1];
    NSLog(@"%u", lastIndex);

    [self performSegueWithIdentifier: @"HomeworkDetailsSegue" sender:self];
    view.currentIndex = lastIndex;

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

At this point, I can successfully move on to the next ViewController (HomeworkDetails) but the information is not passed through. I am trying to accomplish this by doing view.currentIndex = lastIndex; but this is not working. How else can I do this? Sorry if I missed anything; I am a beginner at iOS development.

With suggestions:

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


    HomeworkDetails *view =  [[HomeworkDetails alloc] init];


    int lastIndex = [indexPath indexAtPosition:[indexPath length] - 1];
    NSLog(@"%u", lastIndex);

    [self performSegueWithIdentifier: @"HomeworkDetailsSegue" sender:self];
    view.currentIndex = lastIndex;

    [tableView deselectRowAtIndexPath:indexPath animated:YES];



}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"HomeworkDetailsSegue"]) {

        // note that "sender" will be the tableView cell that was selected
        UITableViewCell *cell = (UITableViewCell*)sender;
       NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; //**error**


        HomeworkDetails *vc = (HomeworkDetails*)[segue destinationViewController];
        vc.currentIndex = indexPath.row;
    }
}

Error from Compiler

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What you are describing can best be done in the prepareForSegue:sender: method, instead of didSelectRowAtIndexPath:. In your storyboard, make sure your HomeworkDetailsSegue segue is set up between your tableView's prototype cell and the destination view controller.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{    
    if ([segue.identifier isEqualToString:@"HomeworkDetailsSegue"]) {

        // note that "sender" will be the tableView cell that was selected
        UITableViewCell *cell = (UITableViewCell*)sender;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

        HomeworkDetails *vc = (HomeworkDetails*)[segue destinationViewController];
        vc.currentIndex = indexPath.row;
    }
}

Also, as a matter of style, I would recommend renaming your HomeworkDetails class to HomeworkDetailsViewController. Doing this follows Apple's conventions more closely and makes it clearer what the class represents.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...