iOS Common Controls-uitableviewcell

Source: Internet
Author: User

First, Package cell:

1. Two ways to load xib files

< mode 1> (Newscell is the name of the Xib file )
Nsarray *objects = [[NSBundle mainbundle] loadnibnamed:@ "Newscell" Owner:nil Options:nil];

< way 2>
uinib *nib = [uinib nibwithnibnamed:@ "Newscell" bundle:nil];
Nsarry *objects = [nib Instantiatewithowner:nil options:nil];

Second, set the cell height

1. If the cell height of each row is the same, the cell height should be set with the proxy method
-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath

{

If the height of each line is the same, it can be set in viewdidload: Self.tableView.rowHeight = 80;
Returns the height of the cell corresponding to the Indexpath line
return 80;
}

====================================================================

three, custom cell two ways:  Way One,describe the cell by Xib:    1>It is important to note that the reuse identity is set in the Xib cell (reuse identifier)2>Steps for Encapsulation
New Xib describes what the cell looks like--dealcell.xib new UITableViewCell subclass (Defining a class Dealcell, inheriting from UITableViewCell)--dealcell (encapsulating everything inside xib) Modify the class name of the cell in the Xib (class) for Dealcell.      In Dealcell, you have all the child controls in the Xib (declaring properties, making connections).      Create a new Model Deal (define a class Deal, inherit from NSObject), encapsulate the data. Add model properties to Dealcell (to show the data from the server to the Xib control from the deal)
In the Dealcell: @class Deal; @interface Dealcell:uitableviewcell @property (nonatomic, weak) Iboutlet Uiimageview*IconView; @property (nonatomic, weak) Iboutlet Uiimageview*Dealnewview; @property (nonatomic, weak) Iboutlet UILabel*Titlelabel; @property (nonatomic, weak) Iboutlet UILabel*Buylabel; @property (nonatomic, weak) Iboutlet UILabel*Pricelabel; @property (nonatomic, strong) Deal*deal;+(ID) Dealcell; + (NSString *) ID; #end
In Dealcell, override the Setdeal method (in this method, set the properties of the cell's inner child control based on the model data): #import "DealCell.h" #import "Deal.h" @implementat Ion Dealcell
        - (void) Setdeal: (Deal *) Deal {_deal=deal; //1. Title_titlelabel.text =Deal.title; //2. Number of buyers_buylabel.text = [NSString stringwithformat:@ "Purchase:%d ", deal.buy]; //3. Price_pricelabel.text = [NSString stringwithformat:@ "Price:%F ", Deal.price]; //4. Avatar_iconview.image =[UIImage ImageNamed:deal.icon]; //5. Whether to display the latest_newview.hidden =!deal.isnew; }
        +() Dealcell {return[[NSBundle Mainbundle] loadnibnamed:@ "Dealcell" Owner:nil Options:nil] [0]; }
        + (NSString *) ID {return@ "Deal"; } @end
Provides a class method that returns a Cell object created from Xib.        Add a reuse identifier (such as deal) to the cell in Xib, and then provide a class method that returns a reuse identity.      Provides a class method that returns the height of the cell. 3>use cell to set the height of each row of cells use the reuse identity to get the cell in the cache pool if there is no cell in the cache pool, create a cell delivery model to the cell (this step is-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *Indexpath This method) means to customize the cell by code: 
    1>create a new subclass of UITableViewCell--weibocell. 2>when creating the cell (Initwithstyle:reuseidentifier: Method), add the child controls that need to be used inside the cell. 3>Create a new model class--weibo and add the corresponding data attributes. 4>Add a Weibo model property to Weibocell, and set the properties of the child control while getting the Weibo model data. 5>rewrite the Setweibo: method, where the data from the Weibo model is removed to display on the child controls.
in Deal: @interface deal:nsobject @property (nonatomic, copy) NSString*icon; @property (nonatomic, copy) NSString*title; @property (nonatomic, assign)intbuy; @property (nonatomic, assign)DoublePrice ;        @property (nonatomic, assign) BOOL isnew; #end

iOS Common Controls-uitableviewcell

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.