How to get selected indexPath.section and indexPath.row value in UITableView?

11,182

Use this code..

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:
[[sender superview] tag]];
Share:
11,182
SampathKumar
Author by

SampathKumar

Senior iphone developer

Updated on June 04, 2022

Comments

  • SampathKumar
    SampathKumar about 2 years

    I have insert 2 section and 2 rows in tableview, then i tried to get selected section and row value using UITapGestureRecognizer, but i have to get only indexPath.section value, i want both section and row value. Is it possible to do that? please help me

    Thanks in Advance

    I tried this:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        UITableViewCell *cell=[[UITableViewCell alloc]  initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    
    //    Add the PatientName Label
    
        UILabel *cellTitle=[[UILabel alloc]initWithFrame:CGRectMake(100, 7, 300, 30)];
        cellTitle.userInteractionEnabled = YES;
        cellTitle.tag = indexPath.section;
        [cellTitle setBackgroundColor:[UIColor clearColor]];
        [cellTitle setFont:[UIFont fontWithName:@"Helvetica" size:14]];
        [cellTitle setText:[[cellArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
        [cell.contentView addSubview:cellTitle];
    
        UITapGestureRecognizer *labelTap = [[UITapGestureRecognizer alloc] init];
        [labelTap addTarget:self action:@selector(viewPatient:)];
        [labelTap setDelegate:nil];
        [labelTap setNumberOfTapsRequired:1];
        [cellTitle addGestureRecognizer:labelTap]; // cellTitle add here
        [labelTap release];
    }
    
    -(void)viewPatient:(id)sender
    {
        UITapGestureRecognizer *lSender = sender;
        UILabel *lObjLabel = (UILabel *)[lSender view];
        NSLog(@"tag:%d",lObjLabel.tag); // only get indexpath.section value
    }