Pass Index Number between UITableView List segue(在 UITableView List segue 之间传递索引号)

本文介绍了在 UITableView List segue 之间传递索引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击 UITableView 中的某个列表项时,我想将单击的项的索引传递给下一个详细视图.

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

}

此时,我可以成功移动到下一个ViewController(HomeworkDetails)但信息没有通过.我正在尝试通过执行 view.currentIndex = lastIndex; 来完成此操作,但这不起作用.我还能怎么做?对不起,如果我错过了什么;我是 iOS 开发的初学者.

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.

有建议:

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

推荐答案

您所描述的最好在 prepareForSegue:sender: 方法中完成,而不是 didSelectRowAtIndexPath:.在您的故事板中,确保您的 HomeworkDetailsS​​egue segue 设置在 tableView 的原型单元格和目标视图控制器之间.

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

另外,作为风格问题,我建议将您的 HomeworkDetails 类重命名为 HomeworkDetailsViewController.这样做更符合 Apple 的约定,并更清楚地说明类代表什么.

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.

这篇关于在 UITableView List segue 之间传递索引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!