有时候我们会碰到一个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. VS2012未找到与约束ContractName...匹配的导出

    用VS2012创建ARCGIS插件项目时,提示“未找到与约束ContractName...匹配的导出”,此前一直都是正常的额 经查,发现是近期系统相关更新导致,解决办法有两种途径: 一是删除近期更新的 ...

  2. codeforces C. Design Tutorial: Make It Nondeterministic

    题意:每一个人 都有frist name 和 last name! 从每一个人的名字中任意选择 first name 或者 last name 作为这个人的编号!通过对编号的排序,得到每一个人 最终顺 ...

  3. JS微信分享不好写?来封装一下

    微信开发这块,作为开发工程师来说,一般是避免不了的,也好像发现一些朋友写微信分享都是在每个页面一大把一大把的代码. 代码冗余,即便是复制过来再改也很麻烦. 之前自己封装了一下js,今天来分享一下,希望 ...

  4. Android Http请求

    Android HTTP请求封装代码 /** * This class is the Utils of other classes. */ public class HttpUtil { /** 变量 ...

  5. Direct2D开发:绘制网格

    转载请注明出处:http://www.cnblogs.com/Ray1024 一.引言 最近在使用Direct2D进行绘制工作中,需要实现使用Direct2D绘制网格的功能.在网上查了很多资料,终于实 ...

  6. Android 学习笔记之Volley(七)实现Json数据加载和解析...

    学习内容: 1.使用Volley实现异步加载Json数据...   Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...

  7. js-对象-2

    对象: 对象是一组具有属性和方法的经过组织的数据. 默认对象: 日期对象:(日期基线:1970年1月1日00:00:00) 建立日期对象(实例): 格式:日期对象名称=new Date([日期参数]) ...

  8. An Introduction to Garbage Collection(垃圾回收简介)

    1. Introduction 2. Principles 3. Advantages 4. Disadvantages 5. 常见的垃圾回收技术 5.1. 跟踪式垃圾回收 5.1.1. 基本算法 5 ...

  9. 转载:第四弹!全球首个微信小程序(应用号)开发教程!通宵吐血赶稿,每日更新!

    感谢大家支持!博卡君周末休息了两天,今天又回到战斗状态了.上周五晚上微信放出官方工具和教程了,推荐程序猿小伙伴们都去试一试,结合教程和代码,写写自己的 demo 也不错. 闲话不多说,开始更新! 第七 ...

  10. 连接不上mysqlworkbench问题解决方法

    连接mysqlworkbench出现如下提示:     查看ip     加入host的范围 mysql> select user,host from mysql.user;+--------- ...