views:

1141

answers:

3

Hello,

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.

+1  A: 

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?

pkananen
I want to be able to jump to that specific cell with scrollToRowAtIndexPath:atScrollPosition:animated: at seems to be the correct way to do it.
Reonarudo
Ok, then create an `NSIndexPath` variable, and create a copy for your use so you can call that method in the future.
pkananen
Thats what I have. The problem is that the variable is only instanced when the cell is constructed and if the cell is not visible in the beginning it is not constructed and this value is null
Reonarudo
Why is the indexpath variable only instanced (instantiated) when the cell is constructed? Why can't you instantiate it in the view controller where you are going to issue the scrollTo?
progrmr
Thank you for all your patience
Reonarudo
A: 

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.

Felixyz
Part of your answer isn't true. At a later time after I storing the IndexPath of a specific cell I can still use it's IndexPath since it is referenced in another place.
Reonarudo
@Reonarudo: See edit.
Felixyz
Ok, maybe you are not familiar with what is happening. When I store a cell IndexPath it will always be the same value even if the cell is instanced or not. It is calculated with the number of rows per section and the number os sections that is given to me by the e tableView:cellForRowAtIndexPath: and when I find the specific cells I want I store only their position, I have no intention of storing all the cells because they are way to many.Maybe the best approach would be scroll all down saving all the IP then all up on viewDidLoad? What do you think.Thank you.
Reonarudo
@Reonarudo: What do you mean that you "store a cell indexPath"? What happens is that the table view asks you to *supply* a cell for that particular index path. There is no property on the cell itself that is set, at least none that you should access. "when I find the specific cells I want I store only their position" -- what does this mean? Where do you find them and where do you want to store them. Please make sure to read and understand the documentation for -(UITableViewCell*)dequeueReusableCellWithIdentifier:(NSString*)identifier to understand why what you are doing is not a good idea.
Felixyz
When I fetch the cell contents for each IndexPath, I check if it content number X, if it is, then I want to store its position on the table that is given by the current IndexPath, all cell contents are stored in a coredata db. This is so I can automatically scroll to that specific position when I push a button. Maybe you know a better way of doing this?
Reonarudo
@Reonarudo: I believe kk6yb's answer pretty much covers it.
Felixyz
Thank you for all your patience
Reonarudo
+3  A: 

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.

progrmr
The problem is that the cell is reusable and it is not visible or instanced before tableView:cellForRowAtIndexPath: is called so I don't have any reference to the cell. Since I'm calling row number 0 from section 5 I'm creating the IndexPath and using it in scrollToRowAtIndexPath:atScrollPosition:animated: I'm stuck with this ugly solution, so if you have a better idea please share it.
Reonarudo
Well, if you don't know what cell you want to scroll to, and don't know what index path (row/section) you want to scroll to, it sounds like you can't do it. This may not apply, but in my case I have all the cells in an NSArray, so I can scroll to any cell, but then I am not reusing any either.
progrmr
Thank you for all your patience
Reonarudo