Pass Index Number between 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:.在您的故事板中,确保您的 HomeworkDetailsSegue 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 之间传递索引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 UITableView List segue 之间传递索引号
基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
