Ios_12_tableviewcell Delete Update _ Dream of Red mansions

Source: Internet
Author: User

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//  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) Iboutlet UIBarB Uttonitem *checkallbtn;//Empty-(Ibaction) Trashbtnclick: (Uibarbuttonitem *) sender;//Select all or reverse-(ibaction) CheckAll: ( Uibarbuttonitem *) sender; @end




Beyondviewcontroller.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, returns the dictionary array Nsarray *_arraywithdict;        All object arrays are Nsmutablearray *_girls; The pairs of rows that are ticked should be in the model array//without arrays, as long as a property is added 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];    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 arr] from the plist file according to the full pathAywithcontentsoffile:fullpath];    Then call the custom method and convert the dictionary array into an array of objects [self dictarraytomodel]; }//custom method, converts a dictionary array into an object array-(void) dictarraytomodel{//dictionary array _arraywithdict//Way 1:for in, this case, the controller knows too much, if the model increases the properties, but also to change Code in Controller/* for (nsdictionary *dict in _arraywithdict) {Girl *girl = [Girl girlnamed:dict[@ "name"] Headimg     name:dict[@ "headimg"] verdict:dict[@ "verdict"];     [_girls Addobject:girl]; }/////Method 2: Class method returns an object, parameter as long as a dictionary array can be (Nsdictionary *dict in _arraywithdict) {//parameter as long as the dictionary, so that the controller is        Don't need to know too many things//Girl *girl = [[Girl alloc]initwithdict:dict];                Girl *girl = [Girl girlwithdict:dict];    [_girls Addobject:girl]; }}//data source method, default is a single group, how many rows (each refresh data will call this line)-(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 *) Table  View: (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 = [[UI    Tableviewcell 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; Judging if the model exists in Checkedarray, it 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 mvc-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{//Cancel T        Ableview 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];        Judging, if not checked, then tick, otherwise uncheck//Mode 2: Only modify 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 the customTo check the usability of the Trash button and change the title [self StatusCheck]; }//Proxy method, click line----old version-(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]; Judging, if not checked, tick, otherwise uncheck//mode 1: Manually set the cell style, but this does not conform to the MVC idea ~/* if (cell.accessorytype! = Uitableviewcellaccessorych     Eckmark) {//tick on, also add to the array, remember!          Cell.accessorytype = Uitableviewcellaccessorycheckmark;          [_checkedgirls Addobject:girl];     } else {//uncheck the box and remove cell.accessorytype = Uitableviewcellaccessorydisclosureindicator;     [_checkedgirls Removeobject:girl]; }////Mode 2: Only modify the model, do not move the cell, let TableView reload data, conform to mvc~ if (cell.accessorytype! = Uitableviewcellaccessorychec Kmark) {//join to the selected numberGroup, and then reloaddata! again        [_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 the custom method, check the availability of the Trash button, 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 row group    Indexpath Nsmutablearray *indexpaths = [Nsmutablearray array]; Iterate through the checkedgirls, get the Tick line numbers, and encapsulate them into a indexpath, and then add them to the indexpaths array, with the intent of following the TableView Delete line method used in the for (Girl *girl in _        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 clear the checked array of objects), and then deleterowsatindexpaths (note that the reload premise is that the number of data sources cannot be increased or decreased) [_girls    Removeobjectsinarray:_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 deletes the same number of rows, before the DeleteRows method can be called [_tableview deleterowsatindexpaths:        Indexpaths Withrowanimation:uitableviewrowanimationright]; Finally call the custom method, check the availability of the Trash button, and change the title [Self StatusCheck];} Finally call the custom method, check the availability of the Trash button, and change the title-(void) statuscheck{//If the table has no data, disable the Select All button if (_girls.count = = 0) {_    checkallbtn.enabled = NO; }//Set the number of rows that display checked if (_checkedgirls.count! = 0) {//Disable the Delete button if no row is selected _trashbtn.en        abled = 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 Right-most Select or counterSelect button-(ibaction) Checkall: (Uibarbuttonitem *) Sender {if (_girls.count = = _checkedgirls.count) {//deselect Select all        First modify the model, then reload [_checkedgirls removeallobjects];    [_tableview Reloaddata];        } else {//Select all first modifies the model, then reload//must first empty the checked array and add all [_checkedgirls removeallobjects];        [_checkedgirls Addobjectsfromarray:_girls];    [_tableview Reloaddata];            }//Call the custom method to modify the detection status [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






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.