有时候我们会碰到一个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. SNF开发平台WinForm之十三-单独从服务器上获取PDF文件进行显示-SNF快速开发平台3.3-Spring.Net.Framework

    1运行效果: 2开发实现: 如果需要单独显示PDF文件时用下面代码去实现,指定url地址. 地址: . 获取附件管理的实体对象: List<KeyValuePair<string, obj ...

  2. NSNotification Name 最佳写法

    本文主要借探讨NSNotificationName的最佳写法的机会,学习下extern, static, const, #define 和常量指针与指针常量等的特性和用法. 1.NSNotificat ...

  3. 在 ubuntu 下安装 apache 和 mod_mono ,并测试

    1. 保证 ubuntu 能联网. 2. 打开终端,输入:sudo apt-get install apache2 3. 安装完 apache2 后,打开浏览器,输入:http://localhost ...

  4. The connection to adb is down, and a severe error has occured.(DDMS中没有真机)

    最近老是出现真机用着用着就掉线了,在DDMS中看不到,运行项目出现选择运行机器中也没有,360助手连接电脑OK,任务管理器中没有adb.exe,重启eclipse不行,只能每次重启电脑.按照http: ...

  5. codeforces D. Design Tutorial: Inverse the Problem

    题意:给定一个矩阵,表示每两个节点之间的权值距离,问是否可以对应生成一棵树, 使得这棵树中的任意两点之间的距离和矩阵中的对应两点的距离相等! 思路:我们将给定的矩阵看成是一个图,a 到 b会有多条路径 ...

  6. IE下angularJS页面跳转的bug

    用Angularjs做项目的过程中遇到一种情况:就是在IE浏览器下,当访问网站页面后,点击浏览器中的向左和向右(返回和前进)按钮时,需要点击两次才能正确跳转,但是在chrome及其他浏览器下该bug没 ...

  7. 用JavaScript修改浏览器tab标题

    修改tab或者window的标题,是一项较老的实践.Gmail 用它来提示用户新的聊天消息,当有新的page通过AJAX加载的时候,本站同样用它更新tab title.这是怎样做到的呢?当时是通过设置 ...

  8. Django--models一对多实例

    需求 models一对多表的构建,创建数据,查询数据,数据前端展示等​. 速查 1.创建数据 1 2 all_data = obj.clean()  #{'username': u'user1', ' ...

  9. C#对象与XMl文件之间的相互转换

    C#提供三种序列化方式,分别为: 1.是使用BinaryFormatter进行串行化: 2.使用SoapFormatter进行串行化: 3.使用XmlSerializer进行串行化.其中对于Binar ...

  10. iis7.5错误 配置错误

    iis7.5详细错误   HTTP 错误 500.19 - Internal Server Error无法访问请求的页面,因为该页的相关配置数据无效. 详细错误信息模块 IIS Web Core 通知 ...