Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit & delete multiple rows in UITableView simultaneously

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.

like image 529
simbesi.com Avatar asked Jun 03 '11 12:06

simbesi.com


People also ask

Edit PDF di mana?

Buka dokumen PDF yang akan diedit dan klik kanan. Klik Open With lalu pilih Adobe Acrobat. Klik Tools dan pilih opsi Edit PDF. Silahkan gunakan toolbar yang ada untuk melakukan edit PDF.

Apakah bisa mengetik di PDF?

Klik dua kali kata Text. Setelah itu, Anda bisa memasukkan atau mengetikkan teks. Tikkan tulisan yang Anda ingin tambahkan ke dokumen PDF. Tambahkan tanda tangan (opsional). Jika dokumen PDF memuat formulir yang perlu ditandatangani, Anda juga bisa menggunakan Preview untuk menambahkan tanda tangan sendiri.


1 Answers

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:

  • The IB approach
  • The programmatic approach

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. 
}
like image 102
Jacob Barnard Avatar answered Sep 25 '22 08:09

Jacob Barnard