你的位置:首页 > 软件开发 > 操作系统 > IOS xib在tableview上的简单应用(通过xib自定义cell)

IOS xib在tableview上的简单应用(通过xib自定义cell)

发布时间:2016-03-18 17:00:04
UITableView是一种常用的UI控件,在实际开发中,由于原生api的局限,自定义UITableViewCell十分重要,自定义cell可以通过代码,也可以通过xib。这篇随笔介绍的是通过xib自定义cell。首先通过gif介绍如何创建xib。然后实现代码部分,要注意的是实现 ...

IOS xib在tableview上的简单应用(通过xib自定义cell)

UITableView是一种常用的UI控件,在实际开发中,由于原生api的局限,自定义UITableViewCell十分重要,自定义cell可以通过代码,也可以通过xib。

这篇随笔介绍的是通过xib自定义cell。

首先通过gif介绍如何创建xib。

IOS xib在tableview上的简单应用(通过xib自定义cell)

然后实现代码部分,要注意的是实现代码的同时要使代码与xib相关联。-如图

IOS xib在tableview上的简单应用(通过xib自定义cell)

下面便是代码,一些解释我在代码中注释了。

ViewController.m

//// ViewController.m// CX-Xib在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 dequeueReusableCellWithIdentifier:identifier];    cell = [[[UINib nibWithNibName:@"tableViewCellXib" bundle:nil]instantiateWithOwner:self options:nil]lastObject];    return cell;  }@end

原标题:IOS xib在tableview上的简单应用(通过xib自定义cell)

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。