Pass Index Number between UITableView List segue

穿精又带淫゛_ 提交于 2019-12-18 08:11:12

问题


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;
    }
}


回答1:


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.




回答2:


You should set the index property in your new view prior to segueing to it with performSegueWityIdentifier. And to take it a step further, best practice coding convention calls for setting up the new view using the standard prepareForSegue method.



来源:https://stackoverflow.com/questions/10166509/pass-index-number-between-uitableview-list-segue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!