iphone - 以编程方式选择 tableview 行

标签 iphone objective-c cocoa-touch uitableview

如何以编程方式选择 UITableView 行,以便

- (void)tableView:(UITableView *)tableView 
        didSelectRowAtIndexPath:(NSIndexPath *)indexPath

被执行? selectRowAtIndexPath 只会突出显示该行。

最佳答案

就像亚努斯所说的:

Calling this (-selectRowAtIndexPath:animated:scrollPosition:) method does not cause the delegate to receive a tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath: message, nor will it send UITableViewSelectionDidChangeNotification notifications to observers.

所以你只需要自己调用delegate方法。

例如:

Swift 3 版本:

let indexPath = IndexPath(row: 0, section: 0);
self.tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.none)
self.tableView(self.tableView, didSelectRowAt: indexPath)

ObjectiveC 版本:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath 
                            animated:YES 
                      scrollPosition:UITableViewScrollPositionNone];
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];

Swift 2.3 版本:

 let indexPath = NSIndexPath(forRow: 0, inSection: 0);
 self.tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
 self.tableView(self.tableView, didSelectRowAtIndexPath: indexPath)

关于iphone - 以编程方式选择 tableview 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2035061/

相关文章:

ios - 调试EXC_BAD_ACCESS-Objective-C-Xcode 8.0

iphone - 处理精简版应用程序

ios - 设置背景图像后按钮变为蓝色

iphone - 状态栏/导航栏消失的奇怪故障

ios - 如何使用xcodebuild正确构建armv7静态库

iphone - 自定义 View 上的 AirPlay 按钮

ios - 如何隐藏/显示导航栏中的右键

ios - iOS 9 中的聚光灯扩展

iphone - 多个窗口旋转问题 - iPhone/iPad

iphone - Xcode 5命令行:体系结构“arm64”的无效部署目标“3.0.0”