Eventually:
Girl.h
girl.h// 12_tableview additions and deletions changed//// Created by Beyond on 14-7-27.// Copyright (c) 2014 Com.beyond. All rights reserved.//#import <Foundation/Foundation.h> @interface girl:nsobject//UI controls with a weak, string with copy, Other objects with strong//avatar picture name @property (nonatomic,copy) nsstring *headimgname;//name @property (nonatomic,copy) NSString *name;// Verdict @property (nonatomic,copy) NSString *verdict;//provides a class method, which is a constructor that returns the object that encapsulates the data (return ID also) + (Girl *) girlnamed: (NSString *) Name Headimgname: (nsstring*) Headimgname verdict: (NSString *) verdict;//class method, dictionary to object similar to JavaBean one-time padding + (Girl *) Girlwithdict: (Nsdictionary *) dict;//object method, after setting the object's properties, return Object-(Girl *) Initwithdict: (Nsdictionary *) dict; @end
girl.m
girl.m//12_tableview additions and deletions changed////Created by Beyond on 14-7-27.//Copyright (c) 2014 Com.beyond. All rights reserved.//#import "Girl.h" @implementation girl//provides a class method, which is a constructor that returns the object that encapsulates the data (return ID also) + (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, dictionary to object similar to JavaBean one-time padding + (Girl *) Girlwithdict: (nsdictionary *) dict{//Just call the object's Initwithdict method, the reason for using Self is to be compatible with the subclass return [[Self alloc]initwithdict:dict];} Object method, after setting the object's properties, returns the Object-(Girl *) Initwithdict: (nsdictionary *) dict{//First calls the parent class NSObject's Init method if (self = [super init]) { Set the object's own properties Self.name = dict[@ "name"]; Self.headimgname = dict[@ "headimg"]; Self.verdict = dict[@ "verdict"]; }//Returns the filled object return self; @end
BeyondViewController.h
beyondviewcontroller.h// 13_tableview edit mode//// Created by Beyond on 14-7-28.// Copyright (c) 2014 Com.beyond. All rights reserved.//#import <UIKit/UIKit.h> @interface beyondviewcontroller:uiviewcontroller//Click Delete, Enter edit mode Delete mode-(ibaction) Trashbtnclick: (Uibarbuttonitem *) sender; @property (weak, nonatomic) iboutlet UITableView * TableView, @property (weak, nonatomic) Iboutlet Uibarbuttonitem *trashbtn; @end
Beyondviewcontroller.m
beyondviewcontroller.m//13_tableview edit mode////Created by Beyond on 14-7-28.//Copyright (c) 2014 Com.beyond. All rights reserved.//#import "BeyondViewController.h" #import "Girl.h" @interface Beyondviewcontroller () < uitableviewdatasource,uitableviewdelegate>{///All girls loaded from the plist file, returns 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 to load the plist file [self loadplist]; }//custom method, load plist file-(void) loadplist{//Sg_bundle template code, 1, get. App main package; 2, return the FullPath full path of a file in the main package nsbundle *mainbundle = [NSBundle Mainbundle]; NSString *fullpath = [Mainbundle pathforresource:@ "Girls.plist" oftype:nil]; Returns the dictionary array _arraywithdict = [Nsarray Arraywithcontentsoffile:fullpath] from the plist file according to the full path; Then call the custom method and convert the dictionary array into an array of objects [self dictarraytomodelarray]; }//The custom method, converts the dictionary array,Replace with object array-(void) dictarraytomodelarray{//dictionary array _arraywithdict//Mode 2: Class method returns an object, parameter as long as a dictionary array can be used for (Nsdictionary *di CT in _arraywithdict) {//parameter as long as the dictionary, so that the controller does not have to know too much things//Girl *girl = [[Girl alloc]initwithdict:dict]; Girl *girl = [Girl girlwithdict:dict]; [_girls Addobject:girl]; }}//Click the Delete button to enter edit mode-(ibaction) Trashbtnclick: (Uibarbuttonitem *) Sender {//If you are editing, cancel editing, otherwise start editing if ([_tableview ised Iting]) {//_tableview.editing = NO; [_tableview Setediting:no Animated:yes]; } else {//_tableview.editing = YES; [_tableview Setediting:yes Animated:yes]; }} #pragma mark-proxy method//edit mode, click on a row of the own Delete button when invoking the Commitediting method of the agent, and, as long as the method is implemented, as long as the finger on the screen to the left, will automatically enter the editing mode, pop-up in the cell delete button-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) indexpath{NSLog (@ "commitediting---row--%d", indexpath.row); Get edit mode Uitableviewcelleditingstylestyle = Editingstyle; Gets the row to delete int row = Indexpath.row; If it is delete mode, proceed to if (style! = Uitableviewcelleditingstyledelete) {return; }//Modify the data model first, then update (if you delete some objects in the object array, you can only call TableView Deleterowsatindexpaths method, otherwise error) [_girls Removeobjectatindex:row]; [_tableview Deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade]; Call the custom method, responsible for the button title and other status checks [self StatusCheck]; }//custom method, responsible for button title and other status checks-(void) statuscheck{//If nothing can be deleted, then the delete button disables if (_girls.count = = 0) {_trashbtn.enabled = NO; }}//Proxy method, sort, moverow-(void) TableView: (UITableView *) TableView Moverowatindexpath: (Nsindexpath *) Sourceindexpath Toindexpath: (Nsindexpath *) destinationindexpath{//As long as the implementation of this method, you can automatically drag and drop the cell to arrange the//modify the data, wait until the display again, call Cellforrow will still press the most The new order correctly shows the Girl *girl = [_girls objectAtIndex:sourceIndexPath.row]; [_girls Removeobject:girl]; [_girls Insertobject:girl AtIndex:destinationIndexPath.row]; }//proxy method, click Row, uncheck the highlighted background color when clicked-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{[_tableview Deselectrowatindexpath:indexpath Animated:yes];} #pragma mark-Data source method//Data source method, each group, how many rows-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger ) section{//returns the corresponding number of objects in the array return _girls.count; The data source method, each row of each group should show how the interface (including the encapsulated data), the focus!!! Must be implemented otherwise, terminating app due to uncaught exception ' nsinternalinconsistencyexception ', 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 not in the pool, regenerate one cell/* Cell 4 styles: 1,default left-hand text 2,subtitle Left the text large under the text small 3,value 1 left picture left text large right text small 3,value 2 disgusting left text small right text large */cell =[[UITableViewCell Alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellid]; }//Set unique content in cell 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; returns cell return cell; Proxy method, height per line-(cgfloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{return 83;} @end
Display interface default English change to Chinese
Girls.plist
If the data source (that is, an array of objects) and TableView row deletion is not uniform, an error will be
There's actually a middleware in the cell Contentview