如何获得选中的indexPath.section和indexPath.UITableView中的行值



我在tableview中插入了2个section和2行,然后我试图使用UITapGestureRecognizer获得选定的section和行值,但我必须只获得indexPath.section值,我想要section和行值。这可能吗?请帮帮我

Thanks in Advance

我试过了:

- (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
}

使用此代码…

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:
[[sender superview] tag]];

写下这行…

[cellTitle setText:[[cellArray objectAtIndex:indexPath.row] objectAtIndex:indexPath.section]];

代替

[cellTitle setText:[[cellArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];

最新更新