iphone - 以编程方式选择 tableview 行

如何以编程方式选择 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)

https://stackoverflow.com/questions/2035061/

相关文章:

objective-c - 如何在 UIScrollView 内的 UIWebView 上启用放大功

objective-c - 如何将 NSNumber 转换为 NSString

objective-c - 使用 NSLocalizedString 的最佳实践

ios - 如何截取 UIView 的屏幕截图?

iphone - -ObjC 链接器标志有什么作用?

objective-c - Objective-C 中的 MD5 算法

ios - 如何根据文本长度计算 UILabel 宽度?

iphone - 通过保持纵横比和宽度调整 UIImage 的大小

ios - 快速执行选择器 :withObject:afterDelay: is unavailab

objective-c - iOS 7 sizeWithAttributes : replaceme