Click to delete UITableView or insert a Cell, uitableviewcell

Source: Internet
Author: User

Click to delete UITableView or insert a Cell, uitableviewcell

Recently, when implementing the cell editing function of UITableView, I found that I forgot something. so, let's write a blog to review this knowledge. in this article, we will focus on deleting cells. The inserts are exactly the same, so we won't talk about them separately.

To delete a cell, I have encountered two types: Sliding cell deletion and clicking Delete cell. you must be familiar with slide deletion. Today I will mainly talk about Click Delete. for example:


This is the first time I see how simple it is to send a request, delete the data, and refresh the UITableView. but when I write it like this, I find it wrong.

Then I realized that I was wrong. When we implemented the slide-deletion, we usually deleted the data source in the following proxy method, and then deleted the cell.

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

{

[TableView beginUpdates];

If (editingStyle = UITableViewCellEditingStyleDelete ){

NSLog (@ "delete ");

// First, delete the data source,

[_ ArrremoveObject: _ arr [indexPath. row];

// Second, delete the table cell.

// [TableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationLeft];

[TableView deleteRowsAtIndexPaths: @ [indexPath] withRowAnimation: UITableViewRowAnimationRight];

} Else {

// First, insert the data source

[_ ArrinsertObject: @ "Zhang San" atIndex: indexPath. row];

// Second, insert cell.

[TableView insertRowsAtIndexPaths: @ [indexPath] withRowAnimation: UITableViewRowAnimationLeft];

NSLog (@ "add ");

}

[TableView endUpdates];

}

The functions I want to implement now are actually the same as that of slide-deletion, so I also need to perform the above operations, first Delete the data source, and then delete the cell. what I need to do is send a request, delete it in the background, and keep this data in the database consistent with that on the front-end.

I want to understand, and the rest is encoding. The following is part of the source code.

# Pragma mark ------- UIAlertViewDelegate

-(Void) alertView :( UIAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex

{

MDWGirlInfoViewController * viewController = (MDWGirlInfoViewController *) self. controller;

MDWFocusRequest * request = [[MDWFocusRequestalloc] init];

MDWRequestManager * manager = [MDWRequestManagermanager];

If (buttonIndex = 1 ){

NSString * path = [@ "/status/" stringByAppendingString: _ statusId];

// Step 1: first send a network request to delete data

[Manager sendWithoutResponseRequest: pathmethod: @ "DELETE" paras: nilbody: requestonSuccess: ^ (AFHTTPRequestOperation * operartion, id responseObject ){

// Step 2: delete the data source

NSMutableArray * mutableArray = [NSMutableArrayarrayWithArray: _ modelData];

[MutableArray removeObjectAtIndex: [indexPathrow];

_ ModelData = [NSArrayarrayWithArray: mutableArray];

// Step 3: delete the cell

[Self. tableViewdeleteRowsAtIndexPaths: [NSArrayarrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationFade];

} OnFailure: ^ (AFHTTPRequestOperation * operation, NSError * error ){

}];

}

}


The following APIs are used to insert or delete a specified row (or segment) in UITableView:

  • InsertRowsAtIndexPath: withRowAnimation: Insert a row at a specified position
  • DeleteRowsAtIndexPath: withRowAnimation: delete a specified row
  • InsertSections: withRowAnimation: Insert a section at a specified position
  • DeleteSections: withRowAnimation: delete a specified section

Okay. After writing this code, we can get this function done. Forgive me for not inserting the cell. The principle is the same as deleting the cell .......







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.