Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS how to get, current selected indexpath row on cellForRowAtIndexPath method?

I want to make a link using label in every cell of table.

When the link is clicked, the table will get the [indexpath row] of the cell and we will use the index to match with the array index containing string data. The string will be sent to the next push page.

I'm using UITapGestureRecognizer to tap the label and put parameter to selector method.

How to get the current indexpath row the label on the selected cell? This is my sample code :

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
...
UITapGestureRecognizer *gestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openUrl:) ];
gestureRec.numberOfTouchesRequired = 1;
gestureRec.numberOfTapsRequired = 1;
[cell.listPrice addGestureRecognizer:gestureRec];
[gestureRec release];
...
}


- (void)openUrl:(id)sender
{
NSLog(@"DOwnload URL send >> %@",urlDownloadSend);
DownloadNowController *download =[[DownloadNowController alloc]initWithNibName:@"DownloadNowController" bundle:nil];
[self.navigationController pushViewController:download animated:YES];
[download release]; 
}
like image 208
dagger24 Avatar asked Sep 12 '11 12:09

dagger24


People also ask

How to get the cell for an indexpath in a Tableview?

The correct time to do that is in your table view's "tableView (_:cellForRowAt:)" delegate method, right after you "dequeueReusableCell (withIdentifier:for:)" to get the cell for an indexPath. Right there, you can do this (from your original code fragment):

Can I set the path to a specific row in a cell?

A cell may be used for different rows at differ times during the lifetime of the table view, so you can't permanently set the path into the cell. Further, cells may exist but not correspond to a row (until they're later used).

How to find the indexpath of a view in a project?

Below is the extension source file in it's entirety. You can simply put this file in your project and then use the included indexPathForView (_:) method to find the indexPath that contains any view.


2 Answers

To determine the current selected cell you can use next method of UITableView:

- (NSIndexPath *)indexPathForSelectedRow

But I'm not sure that your cell will be selected after UITapGestureRecognizer fired.

I advice you to store row of the cell directly in gestureRec.view in tag property:

gestureRec.view.tag = indexPath.row;

Then in openUrl you can determine the selected cell by getting value of sender.view.tag

like image 121
Nekto Avatar answered Oct 27 '22 13:10

Nekto


It is not very clear what you want to do.. do you have a link layed out on the UITableViewCell which triggers some other actions?

The UITableViewDelegate gives you some really cool methods called:

– tableView:willSelectRowAtIndexPath:
– tableView:didSelectRowAtIndexPath:
– tableView:willDeselectRowAtIndexPath:
– tableView:didDeselectRowAtIndexPath:

When you tap a cell, the willSelectRowAtIndexPath and didSelectRowAtIndexPath are called - supplying you the currently selected NSIndexPath which you can then use to get the row as follows:

indexPath.row;
like image 40
Faizan S. Avatar answered Oct 27 '22 11:10

Faizan S.