【问题标题】:call didSelectRowAtIndexPath on button click from DetailView on iPad从 iPad 上的 DetailView 调用按钮单击上的 didSelectRowAtIndexPath
【发布时间】:2012-01-12 00:08:00
【问题描述】:

我正在制作一个 iPad SplitView 应用程序,其中左侧有 MasterView 表(横向),右侧有 DetailView(到目前为止没有什么新东西......)。当我单击表格单元格时,detailView 会显示单元格详细信息。 我在 detialView 上还有一个按钮可以向前移动到下一个单元格,还有一个按钮可以向后移动到上一个单元格。

我的问题是,每次单击按钮时,我都想更改左侧表格视图中的单元格选择,以便用户知道他当前所在的单元格。

我尝试调用 tableView: didSelectRowAtIndexPath: 方法,但我似乎无法正确设置 NSIndexPath(我想发送一个整数来指示下一个单元格的编号)。

这里是调用 selectRowAtIndexPath: 方法的代码:

iPadHelloWorldAppDelegate 是 AppDelegate。 masterViewController 是主控制器,上面有表格。

// method to set the image for the previous letter
-(IBAction)previousLetter{
    iPadHelloWorldAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    NSLog(@"IndexOfImage equals: %@", appDelegate.indexOfImage);

    int number = [[appDelegate.indexOfImage substringFromIndex:5] intValue];

    NSString *imageName = [NSString stringWithFormat:@"image%@.png",[NSString stringWithFormat:@"%d", number-1]];
    [self.letterImageView setImage:[UIImage imageNamed:imageName]];
    NSLog(@"Image name: %@", imageName);

    appDelegate.indexOfImage = [imageName substringToIndex:6];       

    NSIndexPath *currentPath = appDelegate.currentPath;
    NSIndexPath *nextPath = [NSIndexPath indexPathForRow:currentPath.row-1 inSection:currentPath.section];

    [masterViewController.tableView selectRowAtIndexPath:nextPath animated:YES scrollPosition:UITableViewScrollPositionNone];    

    NSLog(@"currentPath: %@", currentPath);    
    appDelegate.currentPath = nextPath;        
    NSLog(@"nextPath: %@", nextPath);    

请帮忙...

【问题讨论】:

  • Here 是您的问题得到解决的地方。应该正是您正在寻找的。​​span>
  • 嗨,感谢您的回答,但我已经尝试过了,并且 selectRowAtIndexPath 甚至不会像其他人那样突出显示该行。你的意思是哪个答案?第一个使用“doSomeThing”方法? didSelectRowAtIndex 对我也不起作用...

标签: xcode uitableview ipad


【解决方案1】:

我认为这可能有效。对于您的前进按钮,请使用此代码

NSIndexPath *currentPath = [tableView indexPathForSelectedRow];
NSIndexPath *nextPath = [NSIndexPath indexPathForRow:currentPath.row+1 inSection:currentPath.section];
[tableView selectRowAtIndexPath:nextPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];

对于后退按钮,用 -1 代替 +1。

有关 indexPathForRow:inSection:method,请参阅 NSIndexPath UIKit Additions documentation

【讨论】:

  • 嗨!感谢你的回答。我做了你写的。我得到了 currentPath 并且现在可以增加 nextPath 但它仍然没有选择单元格。
  • 好的。我的最后一行有错字(已更正),您可能对此有所解释。如果您可以在原始问题中发布代码(以显示格式化代码),那可能会有所帮助。确保您通过正确的名称引用 tableView 对象。这就是我能想到的,没有看到一些代码。
  • 这是一个猜测,因为我没有尝试过,但你可以尝试设置 self.tableView.allowsSelectionDuringEditing = YES; (使用适合您的情况的对 tableView 的正确引用。)如果您处于 ini 编辑模式,即使您以编程方式选择单元格,您也可能需要它。
  • 我尝试了 zou 的建议,但它仍然没有改变单元格行选择。我在最初的问题中放了一些代码,这样你就可以看到我/正在做什么......
  • 我不确定你为什么设置 cuurentPath = appDelegate.currentPath。应用程序委托将其作为属性保存看起来不寻常。我建议您遵循我最初的建议并使用 NSIndexPath *currentPath = [tableView indexPathForSelectedRow];或 NSIndexPath *currentPath = [self.tableView indexPathForSelectedRow];这假定 tableView 是与您的 previousLetter 方法相同的对象内的 iVar 或属性。
【解决方案2】:

“tableView:didSelectRowAtIndexPath:”是一个委托方法;你不应该直接调用它。相反,请尝试调用“selectRowAtIndexPath:animated:scrollPosition:”

【讨论】:

  • 你能告诉我如何为 selectRowAtIndexPath: 设置 NSIndexPath 吗?我似乎无法做到这一点。
  • 最简单的方法是 [NSIndexPath indexPathForRow:inSection:]
  • 好吧,这对我不起作用。当我调用“tableView:didSelectRowAtIndexPath:”时什么也没发生。
  • “tableView:didSelectRowAtIndexPath:”是一个委托方法;你不应该直接调用它。相反,请尝试调用“selectRowAtIndexPath:animated:scrollPosition:”
猜你喜欢
  • 1970-01-01
  • 2016-11-19
  • 2012-08-02
  • 2013-11-07
  • 1970-01-01
  • 2019-05-25
  • 1970-01-01
  • 2018-12-11
  • 2019-03-25
相关资源
最近更新 更多