-(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;
}
}
再上两张图: