UITableView 单元格索引路径
谁能告诉我如何获得单元格 IndexPath? 我将 IndexPath 保存在 tableView: cellForRowAtIndexPath: 方法中,但这仅在查看单元格时加载,并且我需要知道 viewDidLoad 方法上的索引路径。这导致了一个空值,因为正如我所说,它仅在单元格被查看一次后才加载其值。
谢谢。
编辑
目的是能够跳转到具有特定编号但与部分和行数不线性的特定单元格。
Can anyone tell me how can I get a cell IndexPath?
I was saving the IndexPath in the tableView: cellForRowAtIndexPath: method but this only loads when the cell is viewed and I need to know its index path on the viewDidLoad method. this resulted in a null value because as I said it only loads its value after the cell was viewed once.
Thank you.
EDIT
The intention is being able to jump to a specific cell that has its specific number but its not linear with the sections and rows count.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(4)
试试这个..
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:EventlistTable_obj];
NSIndexPath *indexPath = [EventlistTable_obj indexPathForRowAtPoint: currentTouchPosition];
NSLog(@"Selected row:%d in section:%d",indexPath.row,indexPath.section);
请像这样调用:
[cell.fourth_image_button addTarget:self action:@selector(Btn_pressed:event:) forControlEvents:UIControlEventTouchUpInside];
在我看来,你回答了自己的问题:你不能。因为执行viewDidLoad时cell没有indexPath。事实上,它也没有稍后的,因为如果您使用单元出队(好词!),那么同一个单元可以在十分之一秒内被重用到另一个索引,您应该这样做。
编辑:好的,我想我明白你想要什么:当你将单元格交给 tableView 时,你正在将 indexPath 保存在某个地方。注意:如果你不这样做,你就无法真正知道特定单元格放置在哪个indexPath,至少如果你按照你应该的方式做事的话。
因此,您可以做的就是简单地在 viewDidLoad 方法中创建所有单元格,将它们存储在一个数组中,然后,当表视图需要它们时,只需从该数组中传递它们即可。
请注意,这不是填充表视图的好方法,因为它会阻止单元格的重复使用。更好的方法是将单元格(纯 UIViews)的内容保存在数组中,并在返回单元格之前将它们作为子项添加到单元格的内容视图中。这将允许单元重用,这对于良好的性能至关重要。如果您的单元格数量不超过屏幕所能容纳的数量,则不必担心重复使用它们。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
使用方法 indexPathForCell:在tableView上,给定一个指向
UITableViewCell
的指针,它返回其indexPath。如果在
viewDidLoad
中尚未设置,您可以将scrollToRowAtIndexPath:atScrollPosition:animated:
放入viewWillAppear
方法中。无论如何,如果视图在加载后可以多次出现,例如当它调用的模态视图控制器退出时,这是一个更好的地方。Use the method indexPathForCell: on the tableView, given a pointer to a
UITableViewCell
it returns its indexPath.You can put the
scrollToRowAtIndexPath:atScrollPosition:animated:
in theviewWillAppear
method if it is not set up yet inviewDidLoad
. This is a better place anyway if the view can appear more than once after being loaded, such as when a modal view controller it invokes resigns.