从uitableview中选择多行并删除

时间:2011-02-10 07:18:15

标签: iphone uitableview delete-row

我在tableview中显示一个项目列表。我需要一次从表中选择和删除多行,有关如何执行此操作的任何资源

2 个答案:

答案 0 :(得分:3)

我假设你的桌子只有一个部分。您可以非常轻松地将此解决方案扩展到多个部分。

  • 将NSMutableSet成员“selectedRows”添加到管理TableView的UIViewController子类
  • - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath中切换indexPath.row在“selectedRows”中的成员身份,如下所示:

NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [self.selectedRows containsObject:rowNsNum] )
    [self.selectedRows removeObject:rowNsNum];
else 
    [self.selectedRows addObject:rowNsNum];
  • 直观地指示选择了一行(例如,将单元格的accessoryType属性设置为UITableViewCellAccessoryCheckmark),或以其他方式直观地修改您的单元格以指示它是选定的行
  • 在您的用户界面中添加“删除”按钮,可以在表格部分页眉/页脚,标题栏,任意位置,连接到名为“deleteRows”的选择器
  • 在您的deleteRows方法中,遍历selectedRows集,构建一个indexPaths数组,从数据模型中删除这些行,然后调用(使用您首选的动画类型):

[self.myTableView deleteRowsAtIndexPaths:arrayOfIndexPathsToDelete withRowAnimation:UITableViewRowAnimationTop];    

编辑: 这是我的完整didSelectRowAtIndexPath方法。正确操作可能需要deselectRowAtIndexPath。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if ( self.editing )
        return;

    [self.myTableView deselectRowAtIndexPath:indexPath animated:YES];

    NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
    if ( [self.selectedRows containsObject:rowNsNum] )
        [self.selectedRows removeObject:rowNsNum];
    else 
        [self.selectedRows addObject:rowNsNum];

    [self.myTableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.2];
}

答案 1 :(得分:0)

#import "ViewController.h"
#import "TableViewCell.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tblView;
@property (strong,nonatomic)NSMutableArray *arryData1,*arryData2;
@property (strong,nonatomic)UIBarButtonItem *edit,*delete;
@end

@implementation ViewController
{
    BOOL Selected;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.arryData1 = [[NSMutableArray alloc] initWithObjects:@"MCA",@"MBA",@"BTech",@"MTech",nil];
        self.arryData2 = [[NSMutableArray alloc] initWithObjects:@"Objective C",@"C++",@"C#",@".net",nil];

    self.edit=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(edit:)];
    self.navigationItem.leftBarButtonItem=self.edit;
    self.delete=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(delete:)];
    self.navigationItem.rightBarButtonItem=self.delete;
    self.tblView.allowsMultipleSelection=YES;

    }

-(void)edit:(id)sender
{
    Selected=YES;
    [self.tblView reloadData];
}
-(void)delete:(id)sender
{
    NSArray *selectedCells = [self.tblView indexPathsForSelectedRows];
    NSMutableIndexSet *indicesToDelete = [[NSMutableIndexSet alloc] init];
    for (NSIndexPath *indexPath in selectedCells) {
        [indicesToDelete addIndex:indexPath.row];
    }
    //arrayFromPlist is NSMutableArray
    [self.arryData1 removeObjectsAtIndexes:indicesToDelete];
    [self.tblView beginUpdates];
    [self.tblView deleteRowsAtIndexPaths:selectedCells withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tblView endUpdates];

}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Remove the row from data model
    [self.arryData1 removeObjectAtIndex:indexPath.row];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }

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



 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier= @"Cell";

        TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
        }
        if (Selected==YES) {
            cell.imageView.image=[UIImage imageNamed:@"trash.png"];
        }
        else
        {
            cell.imageView.image=nil;
        }
        cell.textLabel.text = [self.arryData1 objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [self.arryData2 objectAtIndex:indexPath.row];

        return cell;
    }

@end