UITableView实现Cell的滑动删除

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 

           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath      //当在Cell上滑动时会调用此函数

{

    if(temp == 1 || temp == 2)

        return  UITableViewCellEditingStyleDelete;  //返回此值时,Cell会做出响应显示Delete按键,点击Delete后会调用下面的函数,别给传递UITableViewCellEditingStyleDelete参数

    else 

        return  UITableViewCellEditingStyleNone;   //返回此值时,Cell上不会出现Delete按键,即Cell不做任何响应

}

 

- (void) tableView:(UITableView *)tableView 

commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 

 forRowAtIndexPath:(NSIndexPath *)indexPath   //对选中的Cell根据editingStyle进行操作

{

    if (editingStyle == UITableViewCellEditingStyleDelete

    {

        if (temp == 1)  //将单元格从数据库1中删除

        {

            [[CommonDatainstance] delEntity:[[[CommonDatainstance] gainSelectResult] objectAtIndexPath:indexPath]];

            [[CommonDatainstance] saveDB];

            [[CommonDatainstance] refreshResult:[[CommonDatainstance] gainSelectResult]];

            NSArray *array = [[CommonDatainstance] gainSelectResult].fetchedObjects;

            array =  [[self changeArrayForm:array] copy];

            self.listData = array;

            [myTableView reloadData];

        }

        else if (temp == 2) ////将单元格从数据库2中删除

        {

            [[CommonDatainstance] delEntity:[[[CommonDatainstance] gainRecentResult] objectAtIndexPath:indexPath]];

            [[CommonDatainstance] saveDB];

            [[CommonDatainstance] refreshResult:[[CommonDatainstance] gainRecentResult]];

            NSArray *array = [[CommonDatainstance] gainRecentResult].fetchedObjects;

            array =  [[self changeArrayForm:array] copy];

            self.listData = array;

            [myTableView reloadData];

        }

    }

}

posted @ 2012-05-23 10:08  hellocby  阅读(17961)  评论(0编辑  收藏  举报