Insert and delete cells and sections in uitableview

Source: Internet
Author: User

Insert segment:

-(Void) insertsections :( nsindexset *) Sections withrowanimation :( uitableviewrowanimation) animation;

-(Void) deletesections :( nsindexset *) Sections withrowanimation :( uitableviewrowanimation) animation;

Insert row:

-(Void) insertrowsatindexpaths :( nsarray *) indexpaths withrowanimation :( uitableviewrowanimation) animation;

-(Void) deleterowsatindexpaths :( nsarray *) indexpaths withrowanimation :( uitableviewrowanimation) animation;

If you re-write this method in edit mode:

 1 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 2 { 3     if (editingStyle == UITableViewCellEditingStyleDelete) { 4         // Delete the row from the data source 5         if (indexPath.row == 0) { 6             [self.modeInfomation removeObjectAtIndex:indexPath.section]; 7             [self.modeNameArr removeObjectAtIndex:indexPath.section]; 8             NSIndexSet *sectionIndex = [NSIndexSet indexSetWithIndex:indexPath.section]; 9             [self.tableView deleteSections:sectionIndex withRowAnimation:UITableViewRowAnimationLeft];10         }11         else {12             [[self.modeInfomation objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];13             [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];14         }15     }   16     else if (editingStyle == UITableViewCellEditingStyleInsert) {17         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view18     }   19 }

In normal mode:

1 [self.tableView beginUpdates];2     [[self.modeInfomation objectAtIndex:tag] addObject:@"hehe"];3     NSIndexPath *indexpath = [NSIndexPath indexPathForRow:row + 1 inSection:tag];4     [self.tableView insertRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationRight];5     [self.tableView endUpdates];

Note:

Insert or delete rows or segments, update datasource first, and then insertrows or insertsections! Update both!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.