多种cell混合使用
有时候我们会碰到一个tableView上有多种cell,这个时候就需要定义多种cell,根据条件判断,当满足某个条件的时候选择某个cell
先看plist文件:

Person.h
#import <Foundation/Foundation.h> @interface Person : NSObject @property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *status;
@property (nonatomic, copy) NSString *imageName;
@property (nonatomic, copy) NSString *type; @end
LabelCell.h
#import <UIKit/UIKit.h> @interface LabelCell : UITableViewCell // nameLabel
@property (nonatomic, strong) UILabel *nameLabel; // statusLabel
@property (nonatomic, strong) UILabel *statusLabel; @end
LabelCell.m
#import "LabelCell.h"
@implementation LabelCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self add];
}
return self;
}
- (void)add {
self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
// self.nameLabel.backgroundColor = [UIColor redColor];
self.nameLabel.font = [UIFont fontWithName:@"Helvetica" size:];
// NSLog(@"%@", [UIFont familyNames]);
[self.contentView addSubview:self.nameLabel];
self.statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(, CGRectGetMaxY(self.nameLabel.frame) + , , )];
// self.statusLabel.backgroundColor = [UIColor greenColor];
self.statusLabel.font = [UIFont systemFontOfSize:];
[self.contentView addSubview:self.statusLabel];
}
@end
ImageViewCell.h
#import <UIKit/UIKit.h> @interface ImageViewCell : UITableViewCell @property (nonatomic, strong) UIImageView *myImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *statusLabel; @end
ImageViewCell.m
#import "ImageViewCell.h"
@implementation ImageViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self add];
}
return self;
}
- (void)add {
self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
// self.nameLabel.backgroundColor = [UIColor orangeColor];
self.nameLabel.font = [UIFont fontWithName:@"Helvetica" size:];
[self.contentView addSubview:self.nameLabel];
self.statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(, CGRectGetMaxY(self.nameLabel.frame) + , , )];
self.statusLabel.numberOfLines = ;
// self.statusLabel.backgroundColor = [UIColor blueColor];
self.statusLabel.font = [UIFont systemFontOfSize:];
[self.contentView addSubview:self.statusLabel];
self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.nameLabel.frame) + , , , )];
// self.myImageView.backgroundColor = [UIColor purpleColor];
[self.contentView addSubview:self.myImageView];
}
@end
RootTableViewController.m
#import "RootTableViewController.h"
#import "Person.h"
#import "LabelCell.h"
#import "ImageViewCell.h" @interface RootTableViewController () // 声明大数组存放所有数据
@property (nonatomic, strong) NSMutableArray *allPersonArray; @end @implementation RootTableViewController // 懒加载
- (NSMutableArray *)allPersonArray { if (_allPersonArray == nil) {
_allPersonArray = [NSMutableArray array];
}
return _allPersonArray;
} - (void)viewDidLoad {
[super viewDidLoad]; self.title = @"多种cell";
self.navigationController.navigationBar.barTintColor = [UIColor lightGrayColor]; // 读取数据
[self handleData]; // 第一步:注册cell,有几个自定义cell就注册几个
[self.tableView registerClass:[LabelCell class] forCellReuseIdentifier:@"labelCell"];
[self.tableView registerClass:[ImageViewCell class] forCellReuseIdentifier:@"imageCell"]; } // 读取数据
- (void)handleData { // 1. 获取文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"person.plist" ofType:nil]; // 2. 根据路径读取数据
NSArray *dataArray = [NSArray arrayWithContentsOfFile:path];
//NSLog(@"%@", dataArray); // 3.将数据转为model对象
for (NSDictionary *dict in dataArray) { // 3.1 创建model对象
Person *person = [[Person alloc] init]; // 3.2 使用KVC赋值
[person setValuesForKeysWithDictionary:dict]; // 3.3 将model对象存放到大数组中
[self.allPersonArray addObject:person]; }
//NSLog(@"%@", self.allPersonArray);
} - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.allPersonArray.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 第二步:重用cell
// 获取数据model
Person *person = self.allPersonArray[indexPath.row]; // 判断数据类型,重用相应的cell对象
if ([person.type isEqualToString:@""]) {
LabelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"labelCell" forIndexPath:indexPath]; cell.nameLabel.text = person.name;
cell.statusLabel.text = person.status; return cell;
} ImageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"imageCell" forIndexPath:indexPath]; cell.nameLabel.text = person.name;
cell.statusLabel.text = person.status;
cell.myImageView.image = [UIImage imageNamed:person.imageName]; return cell;
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return ;
} @end
多种cell混合使用的更多相关文章
- IOS-static cell 与 dynamic cell 混合使用
static cell 与 dynamic cell 混合使用 关于静态cell与动态cell的混合使用,google一下便会有很多相关文章,这里也是看过一些前辈的经验(已经忘记具体是从哪篇文章得到的 ...
- iOS开发之多种Cell高度自适应实现方案的UI流畅度分析
本篇博客的主题是关于UI操作流畅度优化的一篇博客,我们以TableView中填充多个根据内容自适应高度的Cell来作为本篇博客的使用场景.当然Cell高度的自适应网上的解决方案是铺天盖地呢,今天我们的 ...
- 转:iOS开发之多种Cell高度自适应实现方案的UI流畅度分析
本篇博客的主题是关于UI操作流畅度优化的一篇博客,我们以TableView中填充多个根据内容自适应高度的Cell来作为本篇博客的使用场景.当然Cell高度的自适应网上的解决方案是铺天盖地呢,今天我们的 ...
- C# HttpWebRequest传递参数多种方式混合使用
在做CS调用第三方接口的时候遇到了这样的一个问题,通过PSOTman调试需要分别在parmas.Headers.Body里面同时传递参数.在网上查询了很多资料,以此来记录一下开发脱坑历程. POSTm ...
- iOS学习之UI自定义cell
一.自定义Cell 为什么需要自定义cell:系统提供的cell满足不了复杂的样式,因此:自定义Cell和自定义视图一样,自己创建一种符合我们需求的Cell并使用这个Cell.如下图所示的这些Cell ...
- iOS学习31之UITableVIewCell自定义
1. 自定义Cell 1> 为什么要自定义Cell UITableView 中系统的Cell共提供了四种默认样式, 分别是: UITableViewCellStyleDefault UITab ...
- UI学习笔记---第十一天UITableView表视图高级-自定义cell
自定义cell,多类型cell混合使用,cell自适应高度 自定义cell就是创建一个UITableViewCell的子类 把cell上的空间创建都封装在子类中,简化viewController中的代 ...
- 浅谈 PHP 中的多种加密技术及代码示例
信息加密技术的分类 单项散列加密技术(不可逆的加密) 属于摘要算法,不是一种加密算法,作用是把任意长的输入字符串变化成固定长的输出串的一种函数 MD5 string md5 ( string $str ...
- 提升布局能力!理解 CSS 的多种背景及使用场景和技巧
CSS background是最常用的CSS属性之一.然而,并不是所有开发人员都知道使用多种背景.这段时间都在关注使用多种背景场景.在本文中,会详细介绍background-image`属性,并结合图 ...
随机推荐
- 基于jQuery HTML5人物介绍卡片特效
基于jQuery HTML5人物介绍卡片特效.这是一款基于jquery.material-cards插件实现的人物介绍卡片形式特效代码.效果图如下: 在线预览 源码下载 实现的代码. html代码 ...
- 在C函数中保存状态:registry、reference和upvalues
C函数可以通过堆栈来和Lua交换数据,但有时候C函数需要在函数体的作用域之外保存某些Lua数据,那么我们想到全局变量或static变量,这样做的缺点是:(1)为Lua设计C函数库时,导致不可重入:(2 ...
- Caching查看窗口
闲来无事,做了一个简约的Caching查看窗口,可以方便的查看本地缓存的使用情况: 下面的URL和VersionNum用来查看某个特定资源的特定版本是否存在,分别输入所需信息,点击“检测”,即可在下面 ...
- Table_EXISTS_ACTION=APPEND时导入数据时
11g对数据泵新增了一些功能.这篇介绍新增的选项DATA_OPTIONS=SKIP_CONSTRAINT_ERRORS. Oracle11g的数据泵新增了一个DATA_OPTIONS选项,目前只有一个 ...
- ActionLink()与jquery更好地结合建造MVC网页:
众所周知,微软的MVC框架提供了一系列Helper以用于创建Ajax的网页. 但是,类似于Ajax.ActionLink()的方法创建的Ajax缺乏足够的灵活性,例如: 页面上有很多选项,我们需要根据 ...
- Android manifest之系统自带的permission
Android manifest之系统自带的permission 本文描述Android系统自带的permission.点击查看:“关于permission的原始定义和说明”.点击查看:“Androi ...
- DirectWrite文字排版——字符串去尾
DirectWrite是 DirectX 家族中专门用来做文本处理的部分,主要配合Direct2D进行渲染工作. 一.字符串去尾介绍 在文字渲染中,不免会遇到字符串去尾的需求.字符串去尾指的是:当字符 ...
- 我也想聊聊 OAuth 2.0 —— 基本概念
这是一篇待在草稿箱半年之久的文章 连我自己都不知道我的草稿箱有多少未发布的文章了.这应该是我在上一家公司未解散之前写的,记得当时是要做一个开发者中心,很不幸. 今天,打开草稿箱有种莫名的伤感,看到这个 ...
- 一个简单的3DTouch、Peek和Pop手势Demo,附github地址
参考文章:http://www.jianshu.com/p/74fe6cbc542b 下载链接:https://github.com/banchichen/3DTouch-PeekAndPopGest ...
- 【Beta阶段】团队源代码管理
0. 快速上手与理解 如果你的团队来了一个新队员,有一台全新的机器,你们是否有一个文档,只要设置了相应的权限,她就可以根据文档,从头开始搭建环境,并成功地把最新.最稳定版本的软件编译出来,并运行必要的 ...