How to get selected index in tableview
I am using a table view. In the below method, I want to set the value of appdelegate.page
to the value of table row index.
if row one is selected, then appdelegate.page = 1
, if row开发者_如何学JAVA two is selected, then appdelegate.page = 2
... So what should I replace with index path in my code so that I will get the selected row index?
Here is my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.page = indexPath;//////what should i put here instead of indexpath
SecondViewController *svc = [[[SecondViewController alloc] init] autorelease];
svc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:svc animated:YES];
}
Create a Button in Cell of Table and set it's Tag with indexpath.row
Then Use:
-(IBAction)YourButton_TouchUpInside:(id)sender
{
}
You would put indexPath.row or indexPath.item.
indexPath.item is preferred.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.page = indexPath.row;
SecondViewController *svc = [[[SecondViewController alloc] init] autorelease];
svc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:svc animated:YES];
}
Use indexPath.row
to get selected row value which will set up page property in app delegate.
精彩评论