dagger24
dagger24

Reputation: 115

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]; 
}

Upvotes: 5

Views: 39086

Answers (6)

Erinson
Erinson

Reputation: 88

The following code can be used to obtain the current selected indexpath row:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {

 NSLog(@"selected tableview row is %ld",(long)indexPath.row);
}

Upvotes: 0

Nekto
Nekto

Reputation: 17877

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

Upvotes: 15

Virat Naithani
Virat Naithani

Reputation: 783

You can use a global variable to keep the value of indexpath.row

store the row in didSelectRow:atIndexPath: method

var = indexPath.row;
[tableView reloadData];

var = indexPath.row;
[tableView reloadData];

then use it in cellForRowAtIndexPath

if(indexPath.row==var){

}

Upvotes: 2

Praveen-K
Praveen-K

Reputation: 3401

you can do like this

in cellForRowAtIndexPath data source method

UIImageView* selectedBg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg_selected.png"]];

cell.backgroundView = [[UIView alloc] initWithFrame: CGRectZero];
cell.selectedBackgroundView = selectedBg;
[cell.backgroundView setNeedsDisplay];
[selectedBg release];

Upvotes: 1

Faizan S.
Faizan S.

Reputation: 8644

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;

Upvotes: 3

Manlio
Manlio

Reputation: 10865

You may implement method – tableView:willSelectRowAtIndexPath: in your UITableViewDelegate. There you can easily obtain information on indexPath.

Upvotes: 1

Related Questions