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.