Sharing of functional UI Technology in the editing status of UITableViewRowAction tableViewcell in iOS

Source: Internet
Author: User

Sharing of functional UI Technology in the editing status of UITableViewRowAction tableViewcell in iOS

 

* TableView: editActionsForRowAtIndexPath: // you can specify multiple buttons for sliding and deleting.

* UITableViewRowAction // click this button to create

* 1. when using some applications, when sliding a line of some contacts, buttons such as delete, pin, and more appear. Before ios8, we all need to implement it ourselves. But when iOS8 arrives, the system has already been written, and only one proxy method and one class are needed.

* 2. iOS8 The Protocol has one more method, and the returned value is the tableView: editActionsForRowAtIndexPath: Method of the array. We can write several buttons in the method and put them in the array to return them. The classes of those buttons are UITableViewRowAction.

* 3. In the UITableViewRowAction class, we can set the button style, displayed text, background color, and button events (events are implemented in the Block)

* 4. In the proxy method, we can create multiple buttons and put them in the array for return. The buttons that are placed first in the array are displayed on the rightmost side, and the last buttons are displayed on the leftmost side.

* 5. Note: if one or more buttons are set, the deletion button that comes with the system disappears...

Layout:

SDViewController. m

 

#import DetailViewController.h#define kCell @cell@interface SDViewController ()@property (nonatomic, retain) NSMutableArray *dataArray;@end@implementation SDViewController- (void)dealloc{    self.dataArray = nil;    [super dealloc];}


 

 

// Configure the button self. navigationItem. rightBarButtonItem = self. editButtonItem on the right;
Self. dataArray = [[NSMutableArray alloc] initWithObjects: @ 1, @ 2, @ 3, @ 4, @ 5, nil]; // register [self. tableView registerClass: [UITableViewCell class] forCellReuseIdentifier: kCell];

Configure partitions and number of rows:
// Number of partitions-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {return 1;} // number of rows-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return [self. dataArray count];}

Set the cell style:

 

 

-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {UITableViewCell * cell = [tableView metadata: kCell forIndexPath: indexPath]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle :( UITableViewCellStyleValue1) reuseIdentifier: kCell];} // cell display content cell. textLabel. text = [self. dataArray objectAtIndex: indexPath. row]; return cell ;}
Set whether to edit:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {    // Return NO if you do not want the specified item to be editable.    return YES;}

 

Set editing status

 

-(Void) tableView :( UITableView *) tableView commitEditingStyle :( UITableViewCellEditingStyle) editingStyle forRowAtIndexPath :( NSIndexPath *) indexPath {if (editingStyle = UITableViewCellEditingStyleDelete) {// Delete the row from the data source // update data [self. dataArray removeObjectAtIndex: indexPath. row]; // update UI [tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationFade];} else if (editingStyle = birthday ){}}
Important:

 

 

// Set multiple buttons to display when sliding-(NSArray *) tableView :( UITableView *) tableView editActionsForRowAtIndexPath :( NSIndexPath *) indexPath {// Add a delete button UITableViewRowAction * deleteAction = [UITableViewRowAction rowActionWithStyle :( optional) title: @ Delete handler: ^ (UITableViewRowAction * action, NSIndexPath * indexPath) {NSLog (@ click Delete); // 1. update Data [self. dataArray removeObjectAtIndex: indexPath. row]; // 2. update UI [tableView deleteRowsAtIndexPaths: @ [indexPath] withRowAnimation :( UITableViewRowAnimationAutomatic)];}]; // delete button color deleteAction. backgroundColor = [UIColor cyanColor]; // Add a pin button UITableViewRowAction * topRowAction = [comment rowActionWithStyle :( comment) title: @ pin top handler: ^ (UITableViewRowAction * action, NSIndexPath * indexPath) {NSLog (@ click top); // 1. update Data [self. dataArray exchangeObjectAtIndex: indexPath. row withObjectAtIndex: 0]; // 2. update UI NSIndexPath * firstIndexPath = [NSIndexPath indexPathForRow: 0 inSection: indexPath. section]; [tableView moveRowAtIndexPath: indexPath toIndexPath: firstIndexPath] ;}]; // set the top button color topRowAction. backgroundColor = [UIColor magentaColor]; // -------- more UITableViewRowAction * moreRowAction = [custom rowActionWithStyle :( optional) title: @ more handler: ^ (UITableViewRowAction * action, NSIndexPath * indexPath) {DetailViewController * detailVC = [[DetailViewController alloc] init]; [self. navigationController pushViewController: detailVC animated: YES];}]; // background effect // moreRowAction. handler = [effectingtwithstyle :( handle)]; // ---------- add UITableViewRowAction * collectRowAction = [UITableViewRowAction rowActionWithStyle: Collect title: @ add handler: ^ (UITableViewRowAction * action, NSIndexPath * indexPath) {UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ message: @ delegate: self cancelButtonTitle: @ otherButtonTitles: nil, nil]; [alertView show]; [alertView release] ;}]; // collect button color collectRowAction. backgroundColor = [UIColor greenColor]; // return @ [deleteAction, topRowAction, moreRowAction, collectRowAction] to the array of the Set buttons; // return @ [deleteAction, topRowAction, collectRowAction];}
Effect:


 


 

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.