IOS_13_tableView editing mode _ Dream of Red Mansions

Source: Internet
Author: User

IOS_13_tableView editing mode _ Dream of Red Mansions

Finally:


Girl. h

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> /// Girl. h // add, delete, modify, and delete 12_tableView /// Created by beyond on 14-7-27. // Copyright (c) 2014 com. beyond. all rights reserved. // # import @ Interface Girl: NSObject // weak is used for the UI control, copy is used for the string, and strong // is used for other objects. The picture name @ property (nonatomic, copy) NSString * headImgName is used; // name @ property (nonatomic, copy) NSString * name; // discriminant @ property (nonatomic, copy) NSString * verdict; // provides a class method, that is, constructor, returns the encapsulated Data Object (or the returned id) + (Girl *) girlNamed :( NSString *) name headImgName :( NSString *) headImgName verdict :( NSString *) verdict; // class method, the dictionary conversion object is similar to the one-time filling + (Girl *) girlWithDict :( NSDictionary *) dict; // object method. After setting the object attributes, the system returns the object-(Girl *) initWithDict :( NSDictionary *) dict; @ end

Girl. m

/// Girl. m // 12_tableView addition, deletion, modification, // Created by beyond on 14-7-27. // Copyright (c) 2014 com. beyond. all rights reserved. // # import "Girl. h "@ implementation Girl // provides a class method, that is, the constructor, which returns the encapsulated Data Object (the returned id can also be) + (Girl *) girlNamed :( NSString *) name headImgName :( NSString *) headImgName verdict :( NSString *) verdict {Girl * girl = [[Girl alloc] init]; girl. name = name; girl. headImgName = headImgName; girl. verdict = verdict; return girl;} // class method. The dictionary conversion object is similar to a javaBean one-time filling + (Girl *) girlWithDict :( NSDictionary *) dict {// only calls the initWithDict method of the object. The reason why self is used is to be compatible with the subclass return [[self alloc] initWithDict: dict];} // object method, after setting the attributes of an object, the returned object-(Girl *) initWithDict :( NSDictionary *) dict {// call the init method of the parent NSObject if (self = [super init]) {// set the object's own property self. name = dict [@ "name"]; self. headImgName = dict [@ "headImg"]; self. verdict = dict [@ "verdict"] ;}// return the filled object return self ;}@ end


BeyondViewController. h

//// BeyondViewController. h // 13_tableView editing mode /// Created by beyond on 14-7-28. // Copyright (c) 2014 com. beyond. all rights reserved. // # import
 
  
@ Interface BeyondViewController: UIViewController // click Delete to enter the delete mode in edit mode-(IBAction) trashBtnClick :( UIBarButtonItem *) sender; @ property (weak, nonatomic) IBOutlet UITableView * tableView; @ property (weak, nonatomic) IBOutlet UIBarButtonItem * trashBtn; @ end
 


BeyondViewController. m

//// BeyondViewController. m // 13_tableView editing mode /// Created by beyond on 14-7-28. // Copyright (c) 2014 com. beyond. all rights reserved. // # import "BeyondViewController. h "# import" Girl. h "@ interface BeyondViewController ()
 
  
{// All girls loaded from the plist file, return the dictionary array NSArray * _ arrayWithDict; // all object arrays NSMutableArray * _ girls ;} @ end @ implementation BeyondViewController-(void) viewDidLoad {[super viewDidLoad]; NSLog (@ "view did load"); // all object arrays _ girls = [NSMutableArray array]; // call the custom method, load the plist file [self loadPlist];} // custom method, load the plist file-(void) loadPlist {// sg_bundle template code, 1, obtain. the main package of the app; 2. the full path of fullPath of a file in the main package is returned. NSBundle * mainBundle = [N SBundle mainBundle]; NSString * fullPath = [mainBundle pathForResource: @ "girls. plist "ofType: nil]; // return the dictionary array _ arrayWithDict = [NSArray arrayWithContentsOfFile: fullPath] based on the full path from the plist file; // call the custom method again, convert a dictionary array to an object array [self dictArrayToModelArray];} // custom method. convert a dictionary array to an object array (void) dictArrayToModelArray {// dictionary array _ arrayWithDict // Method 2: class method returns an object. You only need a dictionary array for the parameter (NSDictionary * dict in _ arrayWithDict) {// you only need a dictionary for the parameter, In this way, the controller does not need to know much. // Girl * girl = [[Girl alloc] initWithDict: dict]; Girl * girl = [Girl girlWithDict: dict]; [_ girls addObject: girl] ;}// click the delete button to enter the edit mode-(IBAction) trashBtnClick :( UIBarButtonItem *) sender {// cancel editing if editing is in progress, otherwise, edit if ([_ tableView isEditing]) {// _ tableView. editing = NO; [_ tableView setEditing: NO animated: YES];} else {// _ tableView. editing = YES; [_ tableView setEditing: YES animated: Y ES] ;}# pragma mark-proxy method // in edit mode, click the delete button of a row to call the commitEditing method of the proxy, after this method is implemented, as long as the finger slides left on the screen, the edit mode is automatically displayed. The delete button in the cell-(void) tableView :( UITableView *) is displayed *) tableView commitEditingStyle :( UITableViewCellEditingStyle) editingStyle forRowAtIndexPath :( NSIndexPath *) indexPath {NSLog (@ "commitEditing --- row -- % d", indexPath. row); // obtain the edit mode UITableViewCellEditingStyle = editingStyle; // obtain the row int row = I NdexPath. row; // if the deletion mode is used, if (style! = UITableViewCellEditingStyleDelete) {return;} // first modify the data model and then update it. (If some objects are deleted from the object array, you can only call the deleteRowsAtIndexPaths method of tableView. Otherwise, an error is returned) [_ girls removeObjectAtIndex: row]; [_ tableView deleteRowsAtIndexPaths: @ [indexPath] withRowAnimation: UITableViewRowAnimationFade]; // call a custom method, responsible for button title and other status checks [self statusCheck];} // custom method, responsible for button title and other status checks-(void) statusCheck {// can be deleted if there is nothing, the delete button disables if (_ girls. count = 0) {_ trashBtn. en Abled = NO ;}// proxy method, sorting, moveRow-(void) tableView :( UITableView *) tableView moveRowAtIndexPath :( NSIndexPath *) sourceIndexPath toIndexPath :( NSIndexPath *) destinationIndexPath {// as long as this method is implemented, the cell can be automatically dragged to arrange // modify the data until it is displayed again, after cellForRow is called, The Girl * girl = [_ girls objectAtIndex: sourceIndexPath will still be correctly displayed in the latest order. row]; [_ girls removeObject: girl]; [_ girls insertObject: girl atIndex: destinationIndexPath. row];} // proxy When you click a row, cancel the highlighted background color-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {[_ tableView deselectRowAtIndexPath: indexPath animated: YES] ;}# pragma mark-data source method // data source method. The number of rows in each group-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {// return the number of objects in the array return _ girls. count;} // data source method. The interface (including encapsulated data) of each row in each group should be displayed !!! Otherwise, Terminating app due to uncaught exception 'failed', reason: 'uitableview dataSource must return a cell from tableView: cellForRowAtIndexPath: '-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * cellID = @ "Beyond"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: cellID]; if (cell = nil) {// if the pool is not obtained, a new four cell/* cell styles are generated: 1. default: left text 2; default: left text 2; subtitle: Left Text 3: Large Text 3, value 1 left graph large right text small 3, value 2 disgusting left text small right text large */cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: cellID];} // set the unique content Girl * girl = [_ girls objectAtIndex: indexPath. row]; cell. textLabel. text = girl. name; cell. imageView. image = [UIImage imageNamed: girl. headImgName]; cell. detailTextLabel. text = girl. verdict; cell. accessoryType = UITableViewCellAccessoryDisclosureIndicator; // return cell;} // proxy method, height of each row-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {return 83;} @ end
 




The display interface is changed to Chinese by default in English.





Girls. plist



If the row deletion of the data source (Object array) and tableView is inconsistent, an error is returned.




The cell actually has a middleware contentView.









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.