Ios_12_tableviewcell Delete Update _ Dream of Red mansions

Source: Internet
Author: User

Finally:



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 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];< C5/>girl.name = name;    Girl.headimgname = Headimgname;    girl.verdict = verdict;    return girl;} class method, dictionary to object similar JavaBean one-time padding + (Girl *) Girlwithdict: (nsdictionary *) dict{    //is just the Initwithdict method of the calling object, The reason for using Self is to be compatible with the sub-class    return [[Self alloc]initwithdict:dict];} The 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//  12_tableview and additions and changes////  Created by Beyond on 14-7-27.//  Copyright (c) 2014 Com.beyond. All rights reserved.//#import <UIKit/UIKit.h> @interface beyondviewcontroller:uiviewcontroller//title @property ( Weak, nonatomic) Iboutlet UILabel *titlestatus;//tableview@property (weak, nonatomic) Iboutlet UITableView *tableView;/ /Empty Button@property (weak, nonatomic) Iboutlet uibarbuttonitem *trashbtn;//Select all reverse Button@property (weak, nonatomic) IBOutl ET uibarbuttonitem *checkallbtn;//empty-(Ibaction) Trashbtnclick: (Uibarbuttonitem *) sender;//Select all or reverse-(IBAction) Checkall: (Uibarbuttonitem *) sender; @end




BeyondViewCo NTROLLER.M

beyondviewcontroller.m//12_tableview additions and deletions changed////Created by Beyond on 14-7-27.//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, return to the dictionary array Nsarray *_arraywithdict;        An array of all objects Nsmutablearray *_girls; The corresponding model array of the row being ticked//does not use arrays, just add a property to the model: record is checked Nsmutablearray *_checkedgirls;}    @end @implementation beyondviewcontroller-(void) viewdidload{[Super Viewdidload];    All object Arrays _girls = [Nsmutablearray array];    Array of rows checked _checkedgirls = [Nsmutablearray array];    Invoke your own definition method and load the plist file [self loadplist]; }//own definition method, load plist file-(void) loadplist{//Sg_bundle template code, 1, get. App Basic Package, 2, return the FullPath full path of a file in the basic package NSBundle *mainbundle    = [NSBundle Mainbundle];        NSString *fullpath = [Mainbundle pathforresource:@ "Girls.plist" oftype:nil]; Returns the dictionary array _arraywithdict = [Nsarray from the Plist file, based on the full path.Arraywithcontentsoffile:fullpath];    Then call yourself to define the method and convert the dictionary array into an array of objects [self dictarraytomodel]; }//himself defines the method, converts the dictionary array into an object array-(void) dictarraytomodel{///dictionary array _arraywithdict//way 1:for in, so that the controller knows too much about things, assuming the model adds properties, also To change the code in the controller/* for (nsdictionary *dict in _arraywithdict) {Girl *girl = [Girl girlnamed:dict[@ "name"] Headi     mgname:dict[@ "headimg"] verdict:dict[@ "verdict"];     [_girls Addobject:girl]; }/////Method 2: Class method returns the object, only a dictionary array can be used for the (Nsdictionary *dict in _arraywithdict) {//Parameters only a dictionary, in this way, the control of the        The system does not need to know too many things//Girl *girl = [[Girl alloc]initwithdict:dict];                Girl *girl = [Girl girlwithdict:dict];    [_girls Addobject:girl]; }}//the data source method, which is a single group by default, how many rows are collectively owned (this line is called each time the data is refreshed)-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: ( Nsinteger) section{//Returns the length of the corresponding dictionary 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{St    atic nsstring *cellid = @ "Beyond";    UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellid]; if (cell = = nil) {//Assuming that the pool is not taken, another 4 styles for cell/* cells are generated: 1,default left-hand text 2,subtitl E The text on the left picture large under the text small 3,value 1 left figure left text large right text small 3,value 2 disgusting left text small right text large */cell = [U    Itableviewcell Alloc]initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:cellid];    }//Set unique content in cell Girl *girl = _girls[indexpath.row];    Cell.textLabel.text = Girl.name;    Cell.imageView.image = [UIImage imageNamed:girl.headImgName];    Cell.detailTextLabel.text = girl.verdict; Inference, assuming that the model exists in Checkedarray, is marked as checked if ([_checkedgirls Containsobject:girl]) {cell.accessOrytype = Uitableviewcellaccessorycheckmark;    } else {cell.accessorytype = Uitableviewcellaccessorydisclosureindicator; }//Returns cell return cell; Proxy method, height per line-(cgfloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{return 83;} Proxy method, click Line, new version number mvc-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{//Cancel        TableView Click on the background highlighted blue [TableView deselectrowatindexpath:indexpath Animated:yes];        Obtain the corresponding data model of the row being clicked Girl *girl = [_girls objectAtIndex:indexPath.row]; Inference, if not checked, then tick, otherwise uncheck//Mode 2: Just change the model, do not move the cell, let TableView reload data can be, in line with mvc~ if ([_checkedgirls Containsobject:girl])    {//uncheck, remove from the tick array and reloaddata [_checkedgirls Removeobject:girl] again;        } else {//joins in the selected array and then reloaddata! again    [_checkedgirls Addobject:girl];       }//Again Reloaddata [_tableview Reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade];      Finally call yourself to define the method, check the availability of Trashbutton, and change the title [self StatusCheck]; }//Proxy method, click line----old version number-(void) Oldversiontableview: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *)        indexpath{//Cancel TableView after clicking the background highlighted blue [TableView deselectrowatindexpath:indexpath Animated:yes];    Get the row clicked UITableViewCell *cell = [TableView Cellforrowatindexpath:indexpath];            Obtain the corresponding data model of the row being clicked Girl *girl = [_girls objectAtIndex:indexPath.row]; Inference, if not checked, tick, otherwise uncheck//mode 1: Manually set the cell style, however, this does not conform to the MVC idea ~/* if (cell.accessorytype! = Uitableviewcellaccessorych     Eckmark) {//tick on, the same time to add to the array, remember!          Cell.accessorytype = Uitableviewcellaccessorycheckmark;          [_checkedgirls Addobject:girl];     } else {//uncheck the same time to remove cell.accessorytype = Uitableviewcellaccessorydisclosureindicator;     [_checkedgirls Removeobject:girl]; }////Mode 2: Just change the model, do not move the cell, let TableView reload data can, conform to mvc~ if (cell.accessorytype! = UitableviewcellaccessorychEckmark) {//join in the selected array, and then reloaddata!        [_checkedgirls Addobject:girl];    [_tableview Reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade];        } else {//uncheck, remove from the tick array and reloaddata [_checkedgirls Removeobject:girl] again;    [_tableview Reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade]; }//Finally call yourself to define the method, check the availability of Trashbutton, and change the title [Self StatusCheck];} Called when the Trash button in toolbar is clicked-(ibaction) Trashbtnclick: (Uibarbuttonitem *) Sender {//mutable array, member is all tick    The selected rows consist of Indexpath nsmutablearray *indexpaths = [Nsmutablearray array]; Traverse the Checkedgirls, get the Tick line number, and encapsulate it into a indexpath, then add to the Indexpaths array, with the intent to use the for (Girl *girl in _) in the back TableView Delete line method        Checkedgirls) {//tick line number int row = [_girls indexofobject:girl];        Encapsulated into Indexpath nsindexpath *indexpath = [Nsindexpath indexpathforrow:row insection:0]; Add to Indexpaths array [indexpaths addObject:indexpath]; }//Modify the model first (remove the checked objects from all the object arrays, and empty the checked object array), and then deleterowsatindexpaths (note that the reload premise is that the number of data sources cannot be added or lowered) [_girls re    Moveobjectsinarray:_checkedgirls];    [_checkedgirls removeallobjects]; DeleteRows The row is deleted, the number of rows remaining must be equal to the number of rows in the data source, meaning that the data source also removes data from the same number of rows, capable of calling the DeleteRows method [_tableview deleterowsatindexpaths:        Indexpaths Withrowanimation:uitableviewrowanimationright]; Finally call yourself to define the method, check the availability of Trashbutton, and change the title [Self StatusCheck];}         Finally call yourself to define the method, check the availability of Trashbutton, and change the title-(void) statuscheck{//If the table has no data, then directly disable the all-select button if (_girls.count = = 0) {    _checkallbtn.enabled = NO; }//sets the number of rows that display checked if (_checkedgirls.count! = 0) {//Assuming no rows are selected, disable the delete button _TRASHBT        n.enabled = YES; Display numbers (the text in the default Bar button item is immutable, so change to a label label) nsstring *titlestatus = [NSString stringwithformat:@ "Red Mansions (%d)", _che        Ckedgirls.count];    _titlestatus.text = Titlestatus;        } else {_trashbtn.enabled = NO; _Titlestatus.text = @ "Red mansions"; }}//Toolbar Select all or reverse button-(ibaction) Checkall: (Uibarbuttonitem *) Sender {if (_girls.count = = _checkedgir        Ls.count) {//deselect change model First, then reload [_checkedgirls removeallobjects];    [_tableview Reloaddata];        } else {//Select all first change the model, then reload//must first empty the checked array, and then all add [_checkedgirls removeallobjects];        [_checkedgirls Addobjectsfromarray:_girls];    [_tableview Reloaddata];            }//Call yourself to define a method to change the detection state [self statusCheck]; } @end





Property List File Girls.plist





Main.storyboard

Because the text of bar button item cannot be changed, it is replaced with a label,

Label does not receive click events, so it can be passed back to button to handle click events






Ios_12_tableviewcell Delete Update _ Dream of Red mansions

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.