IOS xib simple app on TableView (custom cell via Xib)

Source: Internet
Author: User

UITableView is a common UI control that, in real-world development, is important for custom UITableViewCell due to the limitations of native APIs, and custom cells can be passed through code or xib.

This essay is about customizing the cell through Xib.

First, we'll show you how to create xib with GIF.

Then implement the Code section, pay attention to the implementation of the code at the same time to make the code associated with the Xib. -

Here's the code, some explanations I commented on in the code.

Viewcontroller.m

////VIEWCONTROLLER.M//simple application of cx-xib in TableView////Created by Ma C on 16/3/18.//copyright©2016 year Xubaoaichiyu. All rights reserved.//#import "ViewController.h"#import "CXTableViewCell.h"StaticNSString * identifier =@"Cxcellid";@interfaceViewcontroller () <UITableViewDataSource,UITableViewDelegate>@property (nonatomic, strong) UITableView*TableView;@end@implementationViewcontroller#pragmamark-set_and_get-(UITableView *) tableview{if(!_tableview) {_tableview= [[UITableView alloc]initwithframe:cgrectmake (0, -, Cgrectgetwidth (Self.view.frame), -) Style:uitableviewstyleplain]; _tableview.Delegate=Self ; _tableview.datasource=Self ; _tableview.rowheight= -; [_tableview registernib:[uinib Nibwithnibname:@"Tableviewcellxib"Bundle:nil] forcellreuseidentifier:identifier]; }    return_tableview;}#pragmamark-life-(void) viewdidload {[Super viewdidload]; [Self.view AddSubview:self.tableView];}#pragmamark-deledate-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return 1;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{Cxtableviewcell* Cell =[TableView Dequeuereusablecellwithidentifier:identifier]; Cell= [[[[Uinib Nibwithnibname:@"Tableviewcellxib"bundle:nil]instantiatewithowner:self Options:nil]lastobject]; returncell; }@end

cxtableviewcell.m

////CXTABLEVIEWCELL.M//simple application of cx-xib in TableView////Created by Ma C on 16/3/18.//copyright©2016 year Xubaoaichiyu. All rights reserved.//#import "CXTableViewCell.h"@interfaceCxtableviewcell ()//This is where you write the space and then connect the controls and code on the Xib.@property (nonatomic, weak) iboutlet UILabel *Uplabel, @property (nonatomic, weak) Iboutlet UILabel*downlable, @property (nonatomic, weak) Iboutlet Uiimageview*Cximageview;@end@implementationCxtableviewcell-(Instancetype) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) reuseidentifier{ Self=[Super Initwithstyle:style Reuseidentifier:reuseidentifier]; if(self) {//do not add the control to the view//Add to Contentview is the right choice for you.[Self.contentview addsubview:self.                Cximageview];                [Self.contentview AddSubview:self.upLabel];    [Self.contentview addSubview:self.downLable]; }    returnSelf ;}//use Xib to customize the information on cell Xib to be updated here- (void) awakefromnib {self. Cximageview.image= [UIImage imagenamed:@"caishen.jpg"]; Self.upLabel.text=@"Congratulations, Rich ."; Self.downLable.text=@"financial resources to enter"; }- (void) setselected: (BOOL) selected animated: (bool) animated {[Super setselected:selected animated:animated];}@end

The role of xib in real-world development is not limited to this, but more features await your discovery.

IOS xib simple app on TableView (custom cell via Xib)

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.