新手教程之使用Xib自定义UITableViewCell前言首先:什么是UITableView?看图其次:什么是cell?然后:为什么要自定cell,UITableView不是自带的有cell么?因为在日常开发中,系统自带的cell满足不了客户和开发人员的需求(并且每个cell中 ...
新手教程之使用Xib自定义UITableViewCell
前言
首先:什么是UITableView?看图
其次:什么是cell?
然后:为什么要自定cell,UITableView不是自带的有cell么?
因为在日常开发中,系统自带的cell满足不了客户和开发人员的需求(并且每个cell中的内容\大小\样式相同),我们就需要自定义cell来实现更加优化的功能.比如下面这种
最后:怎么自定义cell?
1.创建一个新的项目,在storyboard中拖入两个imageView,两个label
2.在ViewController里面创建UITableView
1 // 2 // ViewController.m 3 // Xib自定义UITableViewCell 4 // 5 // Created by admin on 16/5/16. 6 // Copyright © 2016年 KXZDJ. All rights reserved. 7 // 8 9 #import "ViewController.h"10 11 @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>12 @property (nonatomic, strong) UITableView *tableView;13 @end14 15 @implementation ViewController16 17 - (void)viewDidLoad {18 [super viewDidLoad];19 // Do any additional setup after loading the view, typically from a nib.20 [self config];21 }22 23 - (void)didReceiveMemoryWarning {24 [super didReceiveMemoryWarning];25 // Dispose of any resources that can be recreated.26 }27 28 29 -(void)config {30 //初始化tableView,并给tableView设置frame以及样式31 self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];32 //遵守代理和数据源(因为要用到代理和数据源方法)33 self.tableView.delegate = self;34 self.tableView.dataSource = self;35 //添加到ViewController的视图中36 [self.view addSubview:self.tableView];37 }38 39 /**40 * 返回多少个组(默认是1组,如果只有一组可以不实现这个方法)41 *42 * @param tableView 当前tableView43 *44 * @return 组的个数45 */46 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {47 return 1;48 }49 /**50 * 每一组返回多少行51 *52 * @param tableView 当前tableView53 * @param section 当前组54 *55 * @return 行的个数56 */57 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {58 return 20;59 }60 61 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {62 //指定cell的重用标识符63 static NSString *reuseIdentifier = @"CELL";64 //去缓存池找名叫reuseIdentifier的cell65 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];66 //如果缓存池中没有,那么创建一个新的cell67 if (!cell) {68 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];69 }70 //返回当前cell71 return cell;72 }
原标题:新手教程之使用Xib自定义UITableViewCell
关键词:ie
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。