UITableView Cell IndexPath
Solution 1
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.
Solution 2
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
?
Solution 3
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];
Related videos on Youtube
Leonardo Marques
Updated on July 09, 2022Comments
-
Leonardo Marques almost 2 years
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.
-
Hua-Ying about 14 yearswhy do you need the indexPath in the viewDidLoad method?
-
Leonardo Marques about 14 yearsbecause I need to jump to a specific cell position on my table, that I don't know in what position it will be.
-
Giao about 14 yearsI'm confused. How is the sections and row count for a specific data element in a table not linear? If it's not linear, how does a linear table get displayed?
-
Leonardo Marques about 14 yearsfor example every cell data has a number associated to it that goes from 0 to 9 but there will be 3 sections where the first has 3 elements, the second 6 and the third 1, bus this distribution can vary and it's given by a cell indexpath. so I know that my cell will be number 9 but I have to jump to [2,5]. Hope this made what I meant clear. Thank you.
-
-
Leonardo Marques about 14 yearsI want to be able to jump to that specific cell with scrollToRowAtIndexPath:atScrollPosition:animated: at seems to be the correct way to do it.
-
Leonardo Marques about 14 yearsPart 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.
-
pkananen about 14 yearsOk, then create an
NSIndexPath
variable, and create a copy for your use so you can call that method in the future. -
Leonardo Marques about 14 yearsThats 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
-
Leonardo Marques about 14 yearsOk, 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.
-
Felixyz about 14 years@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.
-
Leonardo Marques about 14 yearsWhen 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?
-
Leonardo Marques about 14 yearsThe 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.
-
progrmr about 14 yearsWell, 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 about 14 yearsWhy 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?
-
Felixyz about 14 years@Reonarudo: I believe kk6yb's answer pretty much covers it.
-
Leonardo Marques about 14 yearsThank you for all your patience
-
Leonardo Marques about 14 yearsThank you for all your patience
-
Leonardo Marques about 14 yearsThank you for all your patience