select multiple rows from uitableview and delete

前端 未结 2 1899
旧时难觅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
    
    0 讨论(0)
  • 2020-12-09 15:06

    I'm assuming your table has just one section. You can extend this solution to multiple sections fairly easily.

    • Add an NSMutableSet member "selectedRows" to your UIViewController subclass that manages your TableView
    • in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath toggle the indexPath.row's membership in "selectedRows", like this:

    NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
    if ( [self.selectedRows containsObject:rowNsNum] )
        [self.selectedRows removeObject:rowNsNum];
    else 
        [self.selectedRows addObject:rowNsNum];
    
    • indicate visually that a row is selected (e.g., set the cell's accessoryType property to UITableViewCellAccessoryCheckmark), or modify your cell visually in some other way to indicate that it is a selected row
    • add a "delete" button to your UI, either in a table section header/footer, your title bar, anywhere, hooked up to a selector called "deleteRows"
    • in your deleteRows method, iterate through the selectedRows set, building up an array of indexPaths, delete these rows from your data model, then call (with your preferred animation type):

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

    EDIT: Here's my full didSelectRowAtIndexPath method. The deselectRowAtIndexPath may be required for correct operation.

    - (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];
    }
    
    0 讨论(0)
提交回复
热议问题