Ios_14_tableviewcontroller_xib Creating and encapsulating custom cells

Source: Internet
Author: User

Eventually:


xib+ Custom cell packaging Standard steps :( by this step , with less effort )


xib+ custom Cell Packaging standard steps: (By this step, with less effort) 1, create a new girlcell.xib, drag a Tableviewcell inside the cell, drag the various controls 2, create a new Girlcell class, Inherit from UITableViewCell 3, change the class name of the cell in the Girlcell.xib interface to Girlcell, and specify the identifer of reuse, general write class name 4, will girlcell.xib all the various controls in the interface,        Connect to class Girlcell so that they become members of the Girlcell Class 5, create a new data Model Girl, the method list is as follows//UI control with 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;        class method, Dictionary-to-object similar to JavaBean-once padding + (Girl *) Girlwithdict: (Nsdictionary *) dict;  Object method, after setting the object's properties, returns the Object-(Girl *) Initwithdict: (Nsdictionary *) dict;         6, the Girlcell of the package, the method list is as follows @property (weak, nonatomic) Iboutlet Uiimageview *headimg;         @property (Weak, nonatomic) Iboutlet UILabel *name;                  @property (Weak, nonatomic) Iboutlet UILabel *verdict;                  Returns the cell height + (cgfloat) cellheight in the Xib interface; Returns the Xib interface write reuse Cellid +(NSString *) cellid;                  Instantiate a Girlcell object + (Girlcell *) Girlcell from the xib loading;  The parameter is the data Model Girl object, populates the values into Xib controls, and returns the Girlcell object-(Girlcell *) Cellwithgirl: (Girl *) after encapsulating the good data; 7, the Cellforrow method in the controller is as follows-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) in Dexpath {//After the cell is encapsulated, the controller knows something very little ~//first from the cache pool, based on the ID to remove the cell class Girlcell *girlcell =         [TableView Dequeuereusablecellwithidentifier:[girlcell Cellid];          if (Girlcell = = nil) {//If the pool is not taken, then the class method, which regenerates a new girlcell,xib interface, specifies the reuse cellid Girlcell = [Girlcell Girlcell];         }//Set unique content in cell Girl *girl = [_girls objectAtIndex:indexPath.row];         The parameter is a Girl object, populates its individual member values into Xib controls, and returns the populated cell Girlcell = [Girlcell cellwithgirl:girl];               return cell return Girlcell; }

standard steps for creating Tableviewcontroller:


Standard steps to create Tableviewcontroller: 1, new project, then, delete ViewController.h and. m files 2,new file Select OC Class, inherited from Uitableviewcontroller 3, Remove the default Viewcontroller 4 to Main.storyboard, drag a yellow tableviewcontroller to Main.storyboard, and change the file ' s Owner for self-established Beyondtableviewcontroller 5, still to Main.storyboard to remove auto-generated cell control 6, run both



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;//class method, dictionary to object similar to JavaBean disposable fill + (Girl *) Girlwithdict: (Nsdictionary *) dict ;//object method, after setting the object's properties, returns the 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//class method, dictionary to object similar to JavaBean one-time padding + (Girl *) Girlwithdict: ( Nsdictionary *) dict{    //Just call the Initwithdict method of the object, the reason is to use self for the subclass to be compatible with    return [[Self alloc]initwithdict:dict];} Object method, after setting the object's properties, returns the Object-(Girl *) Initwithdict: (nsdictionary *) dict{    //must first call 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



Girlcell.xib


GirlCell.h

girlcell.h//14_tableviewcontroller_xib Custom cell////Created by Beyond on 14-7-28.//Copyright (c) 2014 Com.beyond . All rights reserved./* xib+ custom cell packaging standard steps: (By this step, with less effort) 1, create a new girlcell.xib, drag a Tableviewcell inside the cell, drag into the various control 2, Create a new Girlcell class, Inherit from UITableViewCell 3, change the class name of the cell in the Girlcell.xib interface to Girlcell, and specify identifer for reuse, general write class name 4, Connect all the various controls in the Girlcell.xib interface to the class Girlcell so that they become members of the Girlcell Class 5, create a new data Model Girl, the method list is as follows//UI control with weak, string with copy, other objects with St        Rong//Avatar Picture name @property (nonatomic,copy) NSString *headimgname;        Name @property (nonatomic,copy) NSString *name;        Verdict @property (nonatomic,copy) NSString *verdict;        class method, Dictionary-to-object similar to JavaBean-once padding + (Girl *) Girlwithdict: (Nsdictionary *) dict;  Object method, after setting the object's properties, returns the Object-(Girl *) Initwithdict: (Nsdictionary *) dict;         6, the Girlcell of the package, the method list is as follows @property (weak, nonatomic) Iboutlet Uiimageview *headimg;         @property (Weak, nonatomic) Iboutlet UILabel *name; @property (Weak, nonatomIC) Iboutlet UILabel *verdict;                  Returns the cell height + (cgfloat) cellheight in the Xib interface;                  Returns the reuse Cellid + (NSString *) Cellid written on the Xib interface;                  Instantiate a Girlcell object + (Girlcell *) Girlcell from the xib loading;  The parameter is the data Model Girl object, populates the values into Xib controls, and returns the Girlcell object-(Girlcell *) Cellwithgirl: (Girl *) after encapsulating the good data; 7, the Cellforrow method in the controller is as follows-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) in Dexpath {//After the cell is encapsulated, the controller knows something very little ~//first from the cache pool, based on the ID to remove the cell class Girlcell *girlcell =         [TableView Dequeuereusablecellwithidentifier:[girlcell Cellid];          if (Girlcell = = nil) {//If the pool is not taken, then the class method, which regenerates a new girlcell,xib interface, specifies the reuse cellid Girlcell = [Girlcell Girlcell];         }//Set unique content in cell Girl *girl = [_girls objectAtIndex:indexPath.row];  The parameter is a Girl object, populates its individual member values into Xib controls, and returns the populated cell Girlcell = [Girlcell cellwithgirl:girl];       return cell return Girlcell; } */#import <UIKit/UIKit.h> @class Girl; @interface girlcell:uitableviewcell@property (Weak, nonatomic) Iboutlet Uiimageview *headimg; @property (weak, nonatomic) Iboutlet UILabel *name; @property (weak, nonatomic) Iboutlet UILabel *ver dict;//returns the height of the cell in the Xib interface + (CGFloat) cellheight;//returns the Xib interface write Reuse cellid+ (NSString *) cellid;//from Xib to instantiate a Girlcell object + ( Girlcell *) The girlcell;//parameter is the data model Girl object that populates the values into the Xib controls and returns the Girlcell object after encapsulating the data-(Girlcell *) Cellwithgirl: (Girl *) girl;@ End



girlcell.m

////GirlCell.m 14_tableviewcontroller_xib Custom cell////Created by Beyond on 14-7-28.//Copyright (c) 2014 Com.beyond. All rights reserved.//#import "GirlCell.h" #import "Girl.h" @implementation girlcell//returns the height of the cell in the Xib interface + (cgfloat) cellheight{//Check the height of cell in Xib return 80;} Returns the Reuse cellid+ (NSString *) written on the Xib interface cellid{//must be consistent with the interface return @ "Girlcell";} Instantiate a Girlcell object from Xib + (Girlcell *) girlcell{//Mainbundel load xib, the extension does not write. xib nsarray *arrayxibobjects = [[Nsbundl    E Mainbundle] loadnibnamed:@ "Girlcell" Owner:nil Options:nil]; return arrayxibobjects[0];} After wrapping the data, the Girlcell object-(Girlcell *) Cellwithgirl: (Girl *) girl{//front, by connecting the controls in Xib to the Girlcell class, becomes its member property,    In this way, you do not have to use the tag to get every control in xib _name.text = Girl.name;    _headimg.image = [UIImage imageNamed:girl.headImgName];    _verdict.text = girl.verdict; Returns the Girlcell object return self after encapsulating the data;} @end 



Main.storyboard


BeyondTableViewController.h

  beyondtableviewcontroller.h//  14_tableviewcontroller_xib custom cell////  Created by beyond on 14-7-28.  Copyright (c) 2014 Com.beyond. All rights reserved.//#import <uikit/uikit.h>//tableviewcontroller inherit from Uiviewcontroller and have complied with the data source and proxy, Its internal view is TableView, and has already wired the data source and agent/* To create Tableviewcontroller standard Step 1, new project, then, delete ViewController.h and. m files 2,new file Select OC Class, Inherit from Uitableviewcontroller 3, delete the default Viewcontroller 4 in Main.storyboard, drag a yellow tableviewcontroller to Main.storyboard, and change the file ' s owner for self-established Beyondtableviewcontroller 5, still to Main.storyboard to remove the auto-generated cell control 6, run both */@interface Beyondtableviewcontroller:uitableviewcontroller@end


Beyondtableviewcontroller.m

 
beyondtableviewcontroller.m//14_tableviewcontroller_xib Custom cell////Created by Beyond on 14-7-28.//Copyright (c) 2014 Com.beyond. All rights reserved.//#import "BeyondTableViewController.h" #import "Girl.h" #import "GirlCell.h" @interface Beyondtableviewcontroller () {//All girls loaded from the plist file, returns an array of all the objects Nsmutablearray *_girls;} @end @implementation beyondtableviewcontroller//Hide Top status bar-(BOOL) prefersstatusbarhidden{return YES;}        -(void) viewdidload{[Super Viewdidload];        Initializes an array of objects _girls = [Nsmutablearray array];    Plist into an array of objects [self plisttoobjects]; }//plist to Object array-(void) plisttoobjects{//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 nsarray *arraywithdict = [Nsarray Arraywithcontentsoffile:fullpath] from the plist file according to the full path; The class method of the model returns the object, with the parameter as long as a dictionary array for the nsdictionary *dict in ArraywitHDICT) {//parameter as long as the dictionary, so that the controller does not have to know too much things Girl *girl = [Girl girlwithdict:dict];    Add to Object array [_girls Addobject:girl]; }} #pragma mark-Data source Method-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{//Return row Number of return _girls.count;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{//After the cell is encapsulated, The controller knows something very little ~//First remove the cell class from the cache pool, based on the ID girlcell *girlcell = [TableView dequeuereusablecellwithidentifier:[girl    Cell Cellid]];    if (Girlcell = = nil) {//If the pool is not taken, then the class method, which regenerates a new girlcell,xib interface, specifies the reuse cellid Girlcell = [Girlcell Girlcell];    }//Set unique content in cell Girl *girl = [_girls objectAtIndex:indexPath.row];    The parameter is a Girl object, populates its individual member values into Xib controls, and returns the populated cell Girlcell = [Girlcell cellwithgirl:girl];        return cell return Girlcell; } #pragma mark-proxy method//height of each row-(cgfloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (NSINDEXPATH *) INDexpath{//Call the class method to get the row height of the cell in the Xib return [Girlcell cellheight];} After canceling the default click, the blue Highlight background-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{[ Self.tableview Deselectrowatindexpath:indexpath Animated:yes];} @end



Girls.plist



Tableviewcontroller Hierarchy Chart














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.