【问题标题】:How to get selected indexPath.section and indexPath.row value in UITableView?如何在 UITableView 中获取选定的 indexPath.section 和 indexPath.row 值?
【发布时间】:2013-06-12 07:08:19
【问题描述】:

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

提前致谢

我试过这个:

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

【问题讨论】:

    标签: iphone uitableview row tableview


    【解决方案1】:

    使用此代码..

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

    【讨论】:

      【解决方案2】:

      写下这一行..

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

      而不是

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

      【讨论】:

        猜你喜欢
        • 2018-03-27
        • 2014-10-25
        • 2016-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-13
        • 1970-01-01
        • 2017-03-19
        相关资源
        最近更新 更多