如何得到自定义UITableViewCell中的按钮所在的cell的indexPath.row

在创建button的时候可以把indexpath的值给button.tag。点击的时候响应sender.tag.



在自定义UITableViewCell中创建了一个按钮。

想在点击该按钮时知道该按钮所在的cell在TableView中的行数。就是cell的 indexPath.row

两种方法都很好。
-(IBAction):(id)sender
{
 
    NSLog(@"MyRow:%d",[self.tableindexPathForCell:((TableViewCell*)[[sender    superview]superview])].row); //这个方便一点点,不用设置tag。
      NSLog(@"MyRow:%d",((TableViewCell*)[[sendersuperview]superview]).tag);
      //这个需要加载cell时设置tag.不过也很方便。
}

-(UITableViewCell *)tableView:(UITableView *)tableView
              cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //删除cell.contentView中所有内容,避免以下建立新的重复
      int i =[[cell.contentView subviews] count] - 1;
      for(;i>= 0 ; i--)
      {
                [[[cell.contentView subviews] objectAtIndex:i]removeFromSuperview];
      }


      //添加button
      UIButton*but = [UIButton buttonWithType:UIButtonTypeCustom];
      [butsetBackgroundImage:[UIImage imageNamed:@"delete.png"]forState:UIControlStateNormal];
      [butsetFrame:CGRectMake(280, 10, 30, 30)];
      [butsetAlpha:0.8];
      [butaddTarget:self action:@selector(del:)forControlEvents:UIControlEventTouchUpIns ide];
      [cell.contentView addSubview:but];


      //设置Tag为cell对应的indexPath row
      for(id viewin subviews)
      {
                if([view isKindOfClass:[UIButton class]])
                {
                        [view setTag:[indexPath row]];
                        //[cell.contentView bringSubviewToFront:view];
                }
      }
}



//Button事件
-(void)del:(id)sender
{
      for(UITableViewCell *cell in visiblecells)
      {
              if(cell.tag == button.tag)
      {
      //button.tag就是对应的[indexPath.row
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值