百思不得姐-创建cell的几种方式
2016-04-08 07:33
190 查看
1.首先第一种由系统自带样式摆布,必须重写两个方法如下
说明:上面这个方法是返回tableView的数据源数量
说明:上名的方法设置tableView的每一行数据,这里提供几Style样式,比如上面默认的之后一行text显示,还有显示两行数据,左边显示图片等等,我感觉实际项目中也用的不多,一般都是自定义。。。
2第二种通过xib+自定义Cell实现,这种应该是比较常用的
说明:tags很明显是数据源,测试你可以是模拟数据,一般都是通过获取接口数据,然后通过将数据传给自定义Cell来对数据以及一些额外设置如下自定义cell
.h
说明:一般都会在自定义Cell头文件中给出一个模型数据方法,用于接收控制器传来数据就比如你去星巴克只有拿到咖啡你才能做下面的处理撒。。。
.m
说明:这里就是对数据具体的处理了(咖啡拿到手,下面就是你想咋喝就咋喝了,坐着喝,站着喝,躺着喝,随你意),这里面重写了一个setFrame方法,用于配置每个cell的间距。。。。。
练习源码:http://download.csdn.net/detail/u014360817/9484474
#pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 100; }
说明:上面这个方法是返回tableView的数据源数量
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } cell.textLabel.text = [NSString stringWithFormat:@"%@---%zd", [self class], indexPath.row]; return cell; }
说明:上名的方法设置tableView的每一行数据,这里提供几Style样式,比如上面默认的之后一行text显示,还有显示两行数据,左边显示图片等等,我感觉实际项目中也用的不多,一般都是自定义。。。
2第二种通过xib+自定义Cell实现,这种应该是比较常用的
这个首先吗创建一个自定义Cell,勾选创建xib选项,在xib中添加所需控件如下图
其次定义一个标识名字随意起但最好起的有意义一些如下图
最后就是在代码中注册了代码如下:
#pragma mark - 设置tableView一些属性 - (void)setupTableView { static NSString * const GCSTagsId = @"tag"; //注册 [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GCSRecommendTagCell class]) bundle:nil] forCellReuseIdentifier:GCSTagsId]; //行高 self.tableView.rowHeight = 70; //线 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.backgroundColor = GCSRGBColor(223, 223, 223); }
#pragma mark - tableView数据源 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.tags.count; } #pragma mark - 显示每一行的数据 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // GCSRecommendTagCell *cell = [tableView dequeueReusableCellWithIdentifier:GCSTagsId forIndexPath:indexPath]; GCSRecommendTagCell *cell = [tableView dequeueReusableCellWithIdentifier:GCSTagsId]; cell.recommendTag = self.tags[indexPath.row]; return cell; }
说明:tags很明显是数据源,测试你可以是模拟数据,一般都是通过获取接口数据,然后通过将数据传给自定义Cell来对数据以及一些额外设置如下自定义cell
.h
#import <UIKit/UIKit.h> @class GCSRecommendTag; @interface GCSRecommendTagCell : UITableViewCell /**模型数据*/ @property (nonatomic, strong) GCSRecommendTag *recommendTag; @end
说明:一般都会在自定义Cell头文件中给出一个模型数据方法,用于接收控制器传来数据就比如你去星巴克只有拿到咖啡你才能做下面的处理撒。。。
.m
#import "GCSRecommendTagCell.h" #import <UIImageView+WebCache.h> #import "GCSRecommendTag.h" @interface GCSRecommendTagCell() @property (weak, nonatomic) IBOutlet UIImageView *imageListImageView; @property (weak, nonatomic) IBOutlet UILabel *themeNameLabel; @property (weak, nonatomic) IBOutlet UILabel *subNumberLabel; @end @implementation GCSRecommendTagCell #pragma mark -数据的传递 - (void)setRecommendTag:(GCSRecommendTag *)recommendTag { _recommendTag = recommendTag; [self.imageListImageView sd_setImageWithURL:[NSURL URLWithString:recommendTag.image_list] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]]; self.themeNameLabel.text = recommendTag.theme_name; NSString *subNumber = nil; if (recommendTag.sub_number < 10000) { subNumber = [NSString stringWithFormat:@"%zd人订阅", recommendTag.sub_number]; } else { subNumber = [NSString stringWithFormat:@"%zd人订阅", recommendTag.sub_number / 10000]; } self.subNumberLabel.text = subNumber; } #pragma mark - 重新设置frame - (void)setFrame:(CGRect)frame { //x轴坐标 frame.origin.x = 5; frame.size.width -= 2 * frame.origin.x; frame.size.height -= 1; [super setFrame:frame]; }
说明:这里就是对数据具体的处理了(咖啡拿到手,下面就是你想咋喝就咋喝了,坐着喝,站着喝,躺着喝,随你意),这里面重写了一个setFrame方法,用于配置每个cell的间距。。。。。
练习源码:http://download.csdn.net/detail/u014360817/9484474
相关文章推荐
- URL
- 论技术修炼
- 一款java串口调试工具
- IntelliJ Idea 常用快捷键列表
- [LeetCode] Department Highest Salary 系里最高薪水
- 贪心法
- linuxGPIO
- CSS样式_字体
- SFB公开课:TMG/IISARR/Web Application Proxy/发布UC(Lync/SFB)-2-通知
- Java对象引用
- 心灵鸡汤之20160408
- [BZOJ2946][Poi2000]公共串
- 软硬件调试九法:第七条规则 检查插头
- 骨骼动画的实现(OpenGL实现)
- 2005年开启职业黑暗时代——北漂18年(50)
- 减少JAVA GC
- 数据打包
- CSS魔法堂:你一定误解过的Normal flow
- 10. Regular Expression Matching
- 理解 Keystone 核心概念 - 每天5分钟玩转 OpenStack(18)