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];
}
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):
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).
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.
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
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With