UITableView 单元格索引路径

发布于 2024-08-27 16:11:32 字数 226 浏览 4 评论 0原文

谁能告诉我如何获得单元格 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千纸鹤带着心事 2024-09-03 16:11:32

使用方法 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 the viewWillAppear method if it is not set up yet in viewDidLoad. 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.

如梦 2024-09-03 16:11:32

UITableViewCell 没有 NSIndexPath 属性。 UITableViewDataSource 委托定义了请求给定 NSIndexPath 的单元格的方法,但这并不意味着该单元格与 NSIndexPath 具有任何持久关系> 财产。

如果您详细解释一下您想要此房产的用途,也许会有帮助?您想如何处理 viewDidLoad 中单元格的位置?

A UITableViewCell doesn't have an NSIndexPath property. The UITableViewDataSource delegate defines methods that ask for a cell for a given NSIndexPath, but that doesn't mean the cell has any enduring relationship to an NSIndexPath property.

Maybe it would help if you explained more of what you wanted this property for? What do you want to do with the cell's location in viewDidLoad?

胡渣熟男 2024-09-03 16:11:32

试试这个..

    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];

Try this one..

    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);

Do call like this:

[cell.fourth_image_button addTarget:self action:@selector(Btn_pressed:event:) forControlEvents:UIControlEventTouchUpInside];
那片花海 2024-09-03 16:11:32

在我看来,你回答了自己的问题:你不能。因为执行viewDidLoad时cell没有indexPath。事实上,它也没有稍后的,因为如果您使用单元出队(好词!),那么同一个单元可以在十分之一秒内被重用到另一个索引,您应该这样做。

编辑:好的,我想我明白你想要什么:当你将单元格交给 tableView 时,你正在将 indexPath 保存在某个地方。注意:如果你不这样做,你就无法真正知道特定单元格放置在哪个indexPath,至少如果你按照你应该的方式做事的话。

因此,您可以做的就是简单地在 viewDidLoad 方法中创建所有单元格,将它们存储在一个数组中,然后,当表视图需要它们时,只需从该数组中传递它们即可。

请注意,这不是填充表视图的好方法,因为它会阻止单元格的重复使用。更好的方法是将单元格(纯 UIViews)的内容保存在数组中,并在返回单元格之前将它们作为子项添加到单元格的内容视图中。这将允许单元重用,这对于良好的性能至关重要。如果您的单元格数量不超过屏幕所能容纳的数量,则不必担心重复使用它们。

Seems to me like you answered your own question: you can't. Because the cell doesn't have a indexPath when viewDidLoad is executed. And in fact, it doesn't have one later either, because the very same cell could be reused to be at another index within a tenth of a second, if you use cell dequeueing (nice word!), which you should.

EDIT: Ok I think I see what you want: you are saving the indexPath yourself somewhere when you hand the cell to the tableView. Note: if you don't do this, there is no way that you can really know at which indexPath a particular cell is placed, at least not if you are doing things the way you should.

So what you can do is simply create all the cells in your viewDidLoad method, store them in an array, and then, when the table view asks for them, simply hand them over from that array.

Note that this is not a good way of populating a table view, because it prevents cells from being reused. A better way would be to save the contents of the cells (plain UIViews) in the array, and add them as children to the cell's content view before returning the cell. This would allow for cell-reuse, which is crucial for good performance. If you don't have more cells than fits on the screen, you don't have to worry about reusing them however.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文