在UITableView List segue之间传递索引号 [英] Pass Index Number between 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:
。在您的故事板中,确保在tableView的原型单元格和目标视图控制器之间设置 HomeworkDetailsSegue
segue。
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之间传递索引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!