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.
Th开发者_开发问答ank 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.
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.
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
?
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];
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.
精彩评论