非编辑模式下面实现对Cell的删除

-(void)viewDidLoad
{
 self.title = @"Delete testing";
 //添加背景
 UIImageView *back = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
 [back setBackgroundColor:[UIColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:0.8f]];
 [self.view addSubview:back];
 //初始化数组
 array = [[NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",nil]retain];
 table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 440)];
 [table setDelegate:self];
 [table setDataSource:self];
 [table setBackgroundColor:[UIColor clearColor]];
 [self.view addSubview:table];

 [super viewDidLoad];
 
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 return [self.array count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *RootViewControllerCell = @"RootViewControllerCell";
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:RootViewControllerCell];
 if(cell == nil)
 {
  cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:RootViewControllerCell]autorelease];
  //添加button
  UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
  [but setBackgroundImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
  [but setFrame:CGRectMake(280, 10, 30, 30)];
  [but setAlpha:0.8];
  [but addTarget:self action:@selector(del:) forControlEvents:UIControlEventTouchUpInside];
  [cell.contentView addSubview:but];
  
  //添加显示数据的label
  UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
  [lab setTag:101];
  [lab setBackgroundColor:[UIColor clearColor]];
  [cell.contentView addSubview:lab];
 }
 //设置cell的颜色
 UIColor *cellColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.1];
 cell.backgroundColor = cellColor;
 //设置label内容
 UILabel *lab = (UILabel *)[cell.contentView viewWithTag:101];
 lab.text = [array objectAtIndex:[indexPath row]];

 cell.tag = [indexPath row];//设置cell的tag
 cell.selectionStyle = UITableViewCellSelectionStyleGray;//设置选中cell的样式
 //下面是设置每行cell上面的button的tag,这样可以避免cell的重用机制
 NSArray *subviews = [cell.contentView subviews];
 for(id view in subviews)
 {
  if([view isKindOfClass:[UIButton class]])
  {
   [view setTag:[indexPath row]];
   //[cell.contentView bringSubviewToFront:view];
  }
 }
 return cell;
}
-(void)del:(UIButton *)button//删除事件
{
 //UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"删除" message:@"确定删除吗?" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES"];
 NSArray *visiblecells = [self.table visibleCells];
 for(UITableViewCell *cell in visiblecells)
 {
  if(cell.tag == button.tag)
  {
   k = button.tag;
   UIAlertView *alert = [[UIAlertView alloc] init];
   [alert setTitle:@"删除"];
   [alert setMessage:@"确定删除吗?"];
   [alert addButtonWithTitle:@"是"];
   [alert addButtonWithTitle:@"否"];
   [alert setDelegate:self];
   [alert show];
   break;
  }
 }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
 switch (buttonIndex) {
  case 0:{
   NSArray *visiblecells = [self.table visibleCells];
   for(UITableViewCell *cell in visiblecells)
   {
    if(cell.tag == k)
    {
     //[table deleteRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:cell.tag inSection:0],nil] withRowAnimation:UITableViewRowAnimationTop];
     [array removeObjectAtIndex:[cell tag]];
     [table deleteRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:cell.tag inSection:0], nil] withRowAnimation:UITableViewRowAnimationTop];
     [table reloadData];
     break;
    }
   }
  }
   
   break;
  case 1:
   break;
  default:
   break;
 }
}

 

再上两张图:

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值