有时候我们会碰到一个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混合使用的更多相关文章

  1. IOS-static cell 与 dynamic cell 混合使用

    static cell 与 dynamic cell 混合使用 关于静态cell与动态cell的混合使用,google一下便会有很多相关文章,这里也是看过一些前辈的经验(已经忘记具体是从哪篇文章得到的 ...

  2. iOS开发之多种Cell高度自适应实现方案的UI流畅度分析

    本篇博客的主题是关于UI操作流畅度优化的一篇博客,我们以TableView中填充多个根据内容自适应高度的Cell来作为本篇博客的使用场景.当然Cell高度的自适应网上的解决方案是铺天盖地呢,今天我们的 ...

  3. 转:iOS开发之多种Cell高度自适应实现方案的UI流畅度分析

    本篇博客的主题是关于UI操作流畅度优化的一篇博客,我们以TableView中填充多个根据内容自适应高度的Cell来作为本篇博客的使用场景.当然Cell高度的自适应网上的解决方案是铺天盖地呢,今天我们的 ...

  4. C# HttpWebRequest传递参数多种方式混合使用

    在做CS调用第三方接口的时候遇到了这样的一个问题,通过PSOTman调试需要分别在parmas.Headers.Body里面同时传递参数.在网上查询了很多资料,以此来记录一下开发脱坑历程. POSTm ...

  5. iOS学习之UI自定义cell

    一.自定义Cell 为什么需要自定义cell:系统提供的cell满足不了复杂的样式,因此:自定义Cell和自定义视图一样,自己创建一种符合我们需求的Cell并使用这个Cell.如下图所示的这些Cell ...

  6. iOS学习31之UITableVIewCell自定义

    1. 自定义Cell 1> 为什么要自定义Cell UITableView 中系统的Cell共提供了四种默认样式,  分别是: UITableViewCellStyleDefault UITab ...

  7. UI学习笔记---第十一天UITableView表视图高级-自定义cell

    自定义cell,多类型cell混合使用,cell自适应高度 自定义cell就是创建一个UITableViewCell的子类 把cell上的空间创建都封装在子类中,简化viewController中的代 ...

  8. 浅谈 PHP 中的多种加密技术及代码示例

    信息加密技术的分类 单项散列加密技术(不可逆的加密) 属于摘要算法,不是一种加密算法,作用是把任意长的输入字符串变化成固定长的输出串的一种函数 MD5 string md5 ( string $str ...

  9. 提升布局能力!理解 CSS 的多种背景及使用场景和技巧

    CSS background是最常用的CSS属性之一.然而,并不是所有开发人员都知道使用多种背景.这段时间都在关注使用多种背景场景.在本文中,会详细介绍background-image`属性,并结合图 ...

随机推荐

  1. UIView 与 CALayer

    联系: 1. UIView 有个属性 layer,可以返回它的主 CALayer 实例:CALayer *layer = myView.layer 2. 一个 UIView 可以有多个 CALayer ...

  2. 【转】NPOI 单元格级别应用

    NPOI 单元格级别应用A HSSFWorkbook hssfworkbook = new HSSFWorkbook();//初始化一个新的HSSFWorkbook实例 //#region 1.创建一 ...

  3. 关于FileStream读取大文件问题

    小的文本文件(100M以下)直接用File类的ReadAllText()和WriteAllText()方法 这两个方法内部其实就是封装了StreamReader类的ReadToEnd()和Stream ...

  4. 15套精美的免费界面设计 PSD 素材【免费下载】

    在这个集合中,我们聚集15套精美的 PSD 界面设计模板,网页元素,用户界面​​工具包,扁平化图标,APP 应用程序 UI 设计的等等.这些来自优秀设计师的 PSD 源文件素材让其它的设计师们在设计用 ...

  5. 结对编程之Fault、Error、Failure

    1.结对说明 结对对象:刘世麟  博客地址:http://www.cnblogs.com/liushilin/ 双方贡献:1:1 2.题目要求  构造程序,分别是:         •不能触发Faul ...

  6. hive的内部表与外部表创建

    最近才接触Hive.学到了一些东西,就先记下来,免得以后忘了. 1.创建表的语句:Create [EXTERNAL] TABLE [IF NOT EXISTS] table_name [(col_na ...

  7. [C#高级编程]基础知识摘要一

    核心C#: 值类型存储在堆栈中,而引用类型存储在托管堆上. object类型可以用于两个目的: 可以使用object引用绑定任何子类型的对象 object类型执行许多一般用途的基本方法,包括Equal ...

  8. C#函数式程序设计之局部套用与部分应用

    函数式设计的核心与函数的应用以及函数如何作为算法的基本模块有关.利用局部套用技术可以把所有函数看成是函数类的成员,这些函数只有一个形参,有了局部套用,才有部分应用.部分应用是使函数模块化成为可能的两个 ...

  9. [Solution] ASP.NET Identity(1) 快速入门

    本节将介绍: ASP.NET Identity简介 快速入门 扩展 ASP.NET Identity简介 身份管理在ASP.NET中存在很长世间了,ASP.NET 开发团队已经学会了很多从客户的反馈. ...

  10. MyBatis动态SQL使用,传入参数Map中的Key判断

    <select id="" parameterType="Map" resultMap="commodityResultMap" &g ...