select multiple rows from uitableview and delete

前端 未结 2 1908
旧时难觅i
旧时难觅i 2020-12-09 14:29

i am displaying a list of items in a tableview.i need to select and delete multiple rows from the table at a time,any resources on how to do this

2条回答
  •  难免孤独
    2020-12-09 14:56

    #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
    

提交回复
热议问题