Simple Application of IOS xib on tableview (using xib to customize cell), tableviewxib

Source: Internet
Author: User

Simple Application of IOS xib on tableview (using xib to customize cell), tableviewxib

UITableView is a common UI control. In actual development, due to the limitations of native APIs, custom UITableViewCell is very important. Custom cells can be either through code or xib.

This article describes how to use xib to customize cells.

First, use gif to introduce how to create xib.

In the Code Section, note that the Code should be associated with xib while implementing the code. -

The following is the code. I have commented out some explanations in the code.

ViewController. m

/// ViewController. m // simple application of CX-Xib in tableView /// Created by ma c on 16/3/18. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "ViewController. h "# import" CXTableViewCell. h "static NSString * identifier = @" cxCellID "; @ interface ViewController () <UITableViewDataSource, UITableViewDelegate> @ property (nonatomic, strong) UITableView * tableView; @ end @ implementation ViewController # pragma mark-set_and_get-(UITableView *) tableView {if (! _ TableView) {_ tableView = [[UITableView alloc] initWithFrame: CGRectMake (0, 20, CGRectGetWidth (self. view. frame), 300) style: UITableViewStylePlain]; _ tableView. delegate = self; _ tableView. dataSource = self; _ tableView. rowHeight = 100; [_ tableView registerNib: [UINib nibWithNibName: @ "tableViewCellXib" bundle: nil] forCellReuseIdentifier: identifier];} return _ tableView ;} # pragma mark-life-(void) viewDidLoad {[super viewDidLoad]; [self. view addSubview: self. tableView] ;}# pragma mark-deleDate-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return 1;}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {CXTableViewCell * cell = [tableView metadata: identifier]; cell = [[[UINib nibWithNibName: @ "tableViewCellXib" bundle: nil] metadata: self options: nil] lastObject]; return cell;} @ end

CXTableViewCell. m

/// CXTableViewCell. m // simple application of CX-Xib in tableView /// Created by ma c on 16/3/18. // Copyright©2016 xubaoaichiyu. all rights reserved. // # import "CXTableViewCell. h "@ interface CXTableViewCell () // write space first, and then connect the control on xib with the code @ property (nonatomic, weak) IBOutlet UILabel * upLabel; @ property (nonatomic, weak) IBOutlet UILabel * downLable; @ property (nonatomic, weak) IBOutlet UIImageView * CXimageView; @ end @ implementation CXTableViewCell-(instancetype) initWithStyle :( callback) style reuseIdentifier :( NSString *) reuseIdentifier {self = [super initWithStyle: style reuseIdentifier: reuseIdentifier]; if (self) {// do not add the control to view. // add the control to contentView is your most correct choice [self. contentView addSubview: self. CXimageView]; [self. contentView addSubview: self. upLabel]; [self. contentView addSubview: self. downLable];} return self;} // The information on the xib custom cell xib should be updated here-(void) awakeFromNib {self. CXimageView. image = [UIImage imageNamed: @ "caishen.jpg"]; self. upLabel. text = @ ""; self. downLable. text = @ "caiyuan guangjin";}-(void) setSelected :( BOOL) selected animated :( BOOL) animated {[super setSelected: selected animated: animated];} @ end

In actual development, the role of xib is not limited to this, but more functions are also used for your discovery.

Related Article

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.