Tableview left Slide button tableviewcell custom left Slide button, custom tableviewcell

Source: Internet
Author: User

Tableview left Slide button tableviewcell custom left Slide button, custom tableviewcell

When using tableview, we usually need to display one or more buttons when the cell slides left, but the system only displays one button by default, such as the common delete button, so what should we do when we need to have multiple buttons? Let's look down.

First, let's look at the system buttons (only one button is displayed)

// Set the delete button text (NSString *) tableView :( UITableView *) tableView titleForDeleteConfirmationButtonForRowAtIndexPath :( NSIndexPath *) indexPath {
Return @ "delete";} // click the delete button of the cell to call this method-(void) tableView :( UITableView *) tableView commitEditingStyle :( UITableViewCellEditingStyle) editingStyle forRowAtIndexPath :( nonnull NSIndexPath *) indexPath {
If (editingStyle = UITableViewCellEditingStyleDelete ){
[CallRecordsArr removeObjectAtIndex: indexPath. row];
// Delete the corresponding element from the data source before tableview deletes the corresponding cell
[TableView deleteRowsAtIndexPaths: [NSMutableArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationAutomatic];
[NSKeyedArchiver archiveRootObject: callRecordsArr toFile: CALLRECORDSCACHEPATH];
} If you need to display multiple buttons, refer to the following code (NOTE: When we use a Custom button, the above system default method will be ineffective)-(BOOL) tableView :( UITableView *) tableView canEditRowAtIndexPath :( NSIndexPath *) indexPath {
Returntrue;}-(NSArray *) tableView :( UITableView *) tableView editActionsForRowAtIndexPath :( nonnullNSIndexPath *) indexPath {
UITableViewRowAction * action1 = [UITableViewRowActionrowActionWithStyle: UITableViewRowActionStyleNormaltitle: @ "blacklist added" handler: ^ (UITableViewRowAction * _ Nonnull action, NSIndexPath * _ Nonnull indexPath ){
// Implement the corresponding event in the block
}];
UITableViewRowAction * action2 = [UITableViewRowActionrowActionWithStyle: UITableViewRowActionStyleDestructivetitle: @ "delete" handler: ^ (UITableViewRowAction * _ Nonnull action, NSIndexPath * _ Nonnull indexPath ){
[CallRecordsArrremoveObjectAtIndex: indexPath. row];
// Delete the corresponding element from the data source before tableview deletes the corresponding cell
[TableView deleteRowsAtIndexPaths: [NSMutableArrayarrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationAutomatic];
[NSKeyedArchiverarchiveRootObject: callRecordsArrtoFile: CALLRECORDSCACHEPATH];
}]; Action2.backgroundColor = [UIColorpurpleColor]; Note: 1. When rowActionWithStyle is set to watermark, the background color is red by default. When the value is set to UITableViewRowActionStyleNormal, the background color is light gray by default, you can use. backgroundColor settings; 2. When the operation performed by the left-Slide button involves updating the data source and page, update the data source first. In the update view, otherwise, no response will occur. UITableViewRowAction * toTop = [UITableViewRowActionrowActionWithStyle: tip: @ "TOP" handler: ^ (UITableViewRowAction * _ Nonnull action, NSIndexPath * _ Nonnull indexPath ){
// Update data
[CallRecordsArrexchangeObjectAtIndex: indexPath. rowwithObjectAtIndex: 0];
// Update page
NSIndexPath * firstIndexPath = [NSIndexPathindexPathForRow: 0 inSection: indexPath. section];
[TableView moveRowAtIndexPath: indexPath toIndexPath: firstIndexPath];
}]; // The order in which UITableViewRowAction objects are put determines the order of corresponding buttons in cell.
Return @ [toTop, action2, action1];} The following figure is displayed:

 

Here, we need to add"

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

{

} "This method, and I found that this method can also achieve the expected function without it during the experiment test, and did not find any bugs. Therefore, this summary method is for reference only, if any great God knows this, please kindly advise!

 

Related Article

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.