6

In my app I need to delete multiple rows in a table, edit the table and get a check box beside the table. When checked then the table cells are deleted. It is like the iPhone message app. How can I do this, please help me.

theChrisKent
  • 15,029
  • 3
  • 61
  • 62
simbesi.com
  • 1,539
  • 3
  • 17
  • 27
  • possible duplicate of [select multiple rows from uitableview and delete](http://stackoverflow.com/questions/4954393/select-multiple-rows-from-uitableview-and-delete) – jscs Jun 03 '11 at 18:05
  • Other duplicates: [1](http://stackoverflow.com/questions/6222661/how-to-delete-multiple-rows-in-table-view) [2](http://stackoverflow.com/questions/2727302/) [3](http://stackoverflow.com/questions/2949488/) [4](http://stackoverflow.com/questions/5973756/) – jscs Jun 03 '11 at 18:06

3 Answers3

18

If I understand your question correctly, you essentially want to mark UITableViewCells in some way (a checkmark); then, when the user taps a master "Delete" button, all marked UITableViewCells are deleted from the UITableView along with their corresponding data source objects.

To implement the checkmark portion, you might consider toggling between UITableViewCellAccessoryCheckmark and UITableViewCellAccessoryNone for the UITableViewCell's accessory property. Handle touches in the following UITableViewController delegate method:

- (void)tableView:(UITableView *)tableView
  didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath];
    if (c.accessoryType == UITableViewCellAccessoryCheckmark) {
        [c setAccessoryType:UITableViewCellAccessoryNone];
    }
    //else do the opposite

}

You might also look at this post regarding custom UITableViewCells if you're wanting a more complex checkmark.

You can set up a master "Delete" button two ways:

In either case, eventually a method must be called when the master "Delete" button is pressed. That method just needs to loop through the UITableViewCells in the UITableView and determined which ones are marked. If marked, delete them. Assuming just one section:

NSMutableArray *cellIndicesToBeDeleted = [[NSMutableArray alloc] init];
for (int i = 0; i < [tableView numberOfRowsInSection:0]; i++) {
    NSIndexPath *p = [NSIndexPath indexPathWithIndex:i];
    if ([[tableView cellForRowAtIndexPath:p] accessoryType] == 
        UITableViewCellAccessoryCheckmark) {
        [cellIndicesToBeDeleted addObject:p];
        /*
            perform deletion on data source
            object here with i as the index
            for whatever array-like structure
            you're using to house the data 
            objects behind your UITableViewCells
        */
    }
}
[tableView deleteRowsAtIndexPaths:cellIndicesToBeDeleted
                 withRowAnimation:UITableViewRowAnimationLeft];
[cellIndicesToBeDeleted release];

Assuming by "edit" you mean "delete a single UITableViewCell" or "move a single UITableViewCell," you can implement the following methods in the UITableViewController:

- (void)viewDidLoad {
    [super viewDidLoad];

    // This line gives you the Edit button that automatically comes with a UITableView
    // You'll need to make sure you are showing the UINavigationBar for this button to appear
    // Of course, you could use other buttons/@selectors to handle this too
    self.navigationItem.rightBarButtonItem = self.editButtonItem;

}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //perform similar delete action as above but for one cell
    }   
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    //handle movement of UITableViewCells here
    //UITableView cells don't just swap places; one moves directly to an index, others shift by 1 position. 
}
Community
  • 1
  • 1
Jacob M. Barnard
  • 1,347
  • 1
  • 10
  • 24
2

You can put 1 UIButton lets call it "EDIT" and wire up it to IBAction. In IBAction write so you will be able to do as per your requirement.

 -(IBAction)editTableForDeletingRow
 {
      [yourUITableViewNmae setEditing:editing animated:YES];
 }

This will add round red buttons on the left hand corner and you can click on that Delete button will appear click on that and row will be deleted.

You can implement delegate method of UITableView as following.

 -(UITableViewCellEditingStyle)tableView: (UITableView *)tableView editingStyleForRowAtIndexPath: (NSIndexPath *)indexPath
 { 
      //Do needed stuff here. Like removing values from stored NSMutableArray or UITableView datasource 
 }

Hope it helps.

Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
1

you want to be looking for deleteRowsAtIndexPath, with all your code squeezed between [yourTable beginUpdates] & [yourTable endUpdates];

theiOSDude
  • 1,480
  • 2
  • 21
  • 36