自定义UITableView中Delete操作的按钮

以前写过一篇文章:修改UITableView中Delete操作的默认按钮,这只是让默认的Delete按钮显示成了中文的删除按钮而已,如果想将这个删除按钮换成其他图片形式的,怎么办呢?

 

看一下下面的实现。

 

Ios代码   收藏代码
  1. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     static NSString *RootViewControllerCell = @"RootViewControllerCell";  
  4.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:RootViewControllerCell];  
  5.     if(cell == nil)  
  6.     {  
  7.         cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:RootViewControllerCell]autorelease];  
  8.           
  9.         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];  
  10.         [button setBackgroundImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];  
  11.         [button setFrame:CGRectMake(280103030)];  
  12.         [button addTarget:self action:@selector(del:) forControlEvents:UIControlEventTouchUpInside];  
  13.         [cell.contentView addSubview:button];         
  14.     }  
  15.       
  16.     cell.textLabel.text = [array objectAtIndex:[indexPath row]];  
  17.     cell.tag = [indexPath row];  
  18.       
  19.     NSArray *subviews = [cell.contentView subviews];  
  20.     for(id view in subviews)  
  21.     {  
  22.         if([view isKindOfClass:[UIButton class]])  
  23.         {  
  24.             [view setTag:[indexPath row]];  
  25.             [cell.contentView bringSubviewToFront:view];  
  26.         }  
  27.     }  
  28.     return cell;  
  29. }  
  30.   
  31. -(void)del:(UIButton *)button  
  32. {  
  33.     NSArray *visiblecells = [self.table visibleCells];  
  34.     for(UITableViewCell *cell in visiblecells)  
  35.     {  
  36.         if(cell.tag == button.tag)  
  37.         {  
  38.             [array removeObjectAtIndex:[cell tag]];  
  39.             [table reloadData];  
  40.             break;  
  41.         }  
  42.     }  
  43. }  

 

效果图:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值