有时候,我们经常碰到这样的需求

先遵守代理

@interface PublishCollectionCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

创建

_layout = [[UICollectionViewFlowLayout alloc] init];

//        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

//    _collectionView = [UICollectionView new];

_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:_layout];

//    _collectionView.showsHorizontalScrollIndicator = NO;

_collectionView.scrollEnabled = NO;

[_collectionView setBackgroundView:nil];

[_collectionView setBackgroundColor:[UIColor clearColor]];

[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

_collectionView.dataSource = self;

_collectionView.delegate = self;

[self.contentView addSubview:_collectionView];

你可以先获取cell的高度  设置 cell的默认高

+ (CGFloat)cellHeightWithObj:(NSArray *)obj{

if (obj.count == 0) return 0;

PublishLayout *publishLayout = obj[0];

CGFloat cellHeight = 0;

NSInteger row;

if (obj.count <= 0) {

row = 1;

}else{

row = ceilf((float)obj.count/publishLayout.showConutInRow);

}

cellHeight += (25 + 10) *row;

return cellHeight;

}

根据不同的显示 加载不同的数据源分类

- (void)setDataSource:(NSArray *)dataSource {

_dataSource = dataSource;

if (_dataSource.count == 0) return;

switch (_type) {

case PublishLayoutType_SexLimit:{

_layout.minimumInteritemSpacing = 20.0;

_layout.itemSize = CGSizeMake(70, 25);

ccellItemHeight = 20;

[_collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(_label.mas_right).offset(20);

make.top.equalTo(self.contentView);

make.width.mas_equalTo(250);

make.height.mas_equalTo([PublishCollectionCell cellHeightWithObj:_dataSource]);

}];

}

break;

case PublishLayoutType_LocationLimit:{

_layout.minimumInteritemSpacing = 20.0;

_layout.itemSize = CGSizeMake(70, 25);

ccellItemHeight = 20;

[_collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(_label.mas_right).offset(20);

make.top.equalTo(self.contentView);

make.width.mas_equalTo(160);

make.height.mas_equalTo([PublishCollectionCell cellHeightWithObj:_dataSource]);

}];

}

break;

case PublishLayoutType_Server:{

if ([UIScreen mainScreen].bounds.size.width== 320) {

_layout.minimumInteritemSpacing = 10.0;

_layout.itemSize = CGSizeMake(60, 25);

ccellItemHeight = 25;

[_collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(_label.mas_right).offset(20);

make.top.equalTo(self.contentView);

make.width.mas_equalTo(210);

make.height.mas_equalTo([PublishCollectionCell cellHeightWithObj:_dataSource]);

}];

}else{

_layout.minimumInteritemSpacing = 20.0;

_layout.itemSize = CGSizeMake(70, 25);

ccellItemHeight = 25;

[_collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(_label.mas_right).offset(20);

make.top.equalTo(self.contentView);

make.width.mas_equalTo(250);

make.height.mas_equalTo([PublishCollectionCell cellHeightWithObj:_dataSource]);

}];

}

}

break;

case PublishLayoutType_Props:{

_layout.minimumInteritemSpacing = 20.0;

CGFloat itemMaxWidth = 0;

for (Props *props in _dataSource) {

CGFloat width = [props.name getSizeWithFont:DefualtButtonTitileFont constrainedToSize:CGSizeMake(MAXFLOAT, DefualtButtonTitileFontSize)].width;

if (width > itemMaxWidth) {

itemMaxWidth = width;

}

}

PublishLayout *publishLayout = _dataSource[0];

itemMaxWidth = publishLayout.buttonImage.size.width + 5 + itemMaxWidth;

_layout.itemSize = CGSizeMake(itemMaxWidth, 25);

ccellItemHeight = 20;

CGFloat width = _layout.minimumInteritemSpacing + itemMaxWidth*2;

[_collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(_label.mas_right).offset(20);

make.top.equalTo(self.contentView);

make.width.mas_equalTo(width);

make.height.mas_equalTo([PublishCollectionCell cellHeightWithObj:_dataSource]);

}];

}

break;

default:

break;

}

//    _collectionView.collectionViewLayout = layout;

[_collectionView reloadData];

}

展示的时候

_tableView = ({

UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

tableView.backgroundColor = [UIColor clearColor];

tableView.dataSource = self;

tableView.delegate = self;

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

[tableView registerClass:[PublishLimitCell class] forCellReuseIdentifier:NSStringFromClass(PublishLimitCell.class)];

[tableView registerClass:[PublishCollectionCell class] forCellReuseIdentifier:NSStringFromClass(PublishCollectionCell.class)];

[self.view addSubview:tableView];

[tableView mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.equalTo(_addTopicButton.mas_bottom).offset(30);

make.left.right.equalTo(self.view);

make.bottom.equalTo(_shareCollectionView.mas_top).offset(-10);

}];

tableView;

});

用tableView 加载不同的cell 类型  复杂界面首选  整理的tableView 包含colloctionView 的写法

不过首先 可以写一个枚举

typedef NS_ENUM(NSInteger, PublishLayoutType) {

PublishLayoutType_SexLimit = 0,

PublishLayoutType_LocationLimit,

PublishLayoutType_Server,

PublishLayoutType_Props,

};

根据不同的类型 加载不同的数据源

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return _store.numberOfRows;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

PublishCollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(PublishCollectionCell.class) forIndexPath:indexPath];

switch (indexPath.row) {

case 0:

cell.label.text = @"对方性别";

cell.type = PublishLayoutType_SexLimit;

cell.dataSource = (NSArray *)_store.sexLimits;

break;

case 1:

cell.label.text = @"对方位置";

cell.type = PublishLayoutType_LocationLimit;

cell.dataSource = (NSArray *)_store.locationLimits;

break;

case 2:

cell.label.text = @"寻求服务";

cell.type = PublishLayoutType_Server;

cell.dataSource = (NSArray *)_store.serviceList;

break;

case 3:

cell.label.text = @"预算赏金";

cell.type = PublishLayoutType_Props;

cell.dataSource = (NSArray *)_store.cards;

break;

default:

break;

}

[cell setClickedBlock:^(PublishLayout *_layout) {

if (_layout.layoutType == PublishLayoutType_Props) {

if (_layout.isSelected) {

_layout.isSelected = !_layout.isSelected;

[_store updateList:_layout];

[_tableView reloadData];

} else {

[CountControlView showChooseNumInSubVC:self withCommitBlock:^(NSInteger chooseNum) {

_layout.propsNumber = chooseNum;

_layout.isSelected = !_layout.isSelected;

[_store updateList:_layout];

[_tableView reloadData];

}];

}

} else {

if (_layout.layoutType == PublishLayoutType_LocationLimit) {

if (_layout.recruitLimit.limitId.integerValue == _store.locationLimit.integerValue) {

return;

}

}

if (_layout.layoutType == PublishLayoutType_SexLimit) {

if (_layout.recruitLimit.limitId.integerValue == _store.sexLimit.integerValue) {

return;

}

}

_layout.isSelected = !_layout.isSelected;

if (_layout.isSelected && _layout.layoutType==PublishLayoutType_LocationLimit) {

[self resignTitleResponder];

if (!self.store.isLocationSuccess && [AppLogic getCityName].length==0) {

CNShowPromptWithText(@"请打开定位或完善资料");

return ;

} else{

}

}

[_store updateList:_layout];

[_tableView reloadData];

}

}];

return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

if (indexPath.row < 2) {

return 45;

} else if (indexPath.row == 2) {

return [PublishCollectionCell cellHeightWithObj:_store.serviceList];

}else if (indexPath.row == 3) {

return [PublishCollectionCell cellHeightWithObj:_store.cards];

}

return 0;

}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

if ([collectionView isEqual:self.rewardCollectionView]) {

return self.store.propsArray.count;

} else {

return 5;

}

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

if ([collectionView isEqual:self.rewardCollectionView]) {

PublishCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reward" forIndexPath:indexPath];

//        cell.props = self.store.propsArray[indexPath.row];

[cell setProps:self.store.propsArray[indexPath.row] IndexPath:indexPath SelectYes:self.isShake];

[cell setDeleteImgRow:^(NSInteger row) {

[self.store.propsArray removeObjectAtIndex:row];

self.isShake = NO;

if (self.store.propsArray.count > 0) {

self.rewardLabel.hidden = YES;

self.rewardCollectionView.hidden = NO;

} else {

self.rewardLabel.hidden = NO;

self.rewardCollectionView.hidden = YES;

}

[self.rewardCollectionView reloadData];

}];

[cell setIsShake:^(BOOL shake) {

self.isShake = YES;

[self.rewardCollectionView reloadData];

}];

return cell;

} else {

PublishCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"share" forIndexPath:indexPath];

NSString *imageNamed = @"";

switch (indexPath.row) {

case 0:

imageNamed = @"share_weixin";

break;

case 1:

imageNamed = @"share_QZone";

break;

case 2:

imageNamed = @"share_WechatTimeline";

break;

case 3:

imageNamed = @"share_qq";

break;

case 4:

imageNamed = @"share_weibo";

break;

default:

break;

}

if (_store.shareType == indexPath.row) {

imageNamed = [imageNamed stringByAppendingString:@"_sel"];

} else {

imageNamed = [imageNamed stringByAppendingString:@"_nor"];

}

cell.contengImageView.image = [UIImage imageNamed:imageNamed];

return cell;

}

return nil;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

if ([collectionView isEqual:self.shareCollectionView]) {

if (_store.shareType != indexPath.row) {

_store.shareType = indexPath.row;

} else {

_store.shareType = KShareTypeNone;

}

[self.shareCollectionView reloadData];

}

else if([collectionView isEqual:self.rewardCollectionView]){

NSLog(@"%ld",(long)indexPath.row);

}

}

大概的整体思路 就是这样, 最近会写一个demo 然后放到git上 若需要可以评论 @我

动态的计算行高 加载数据源 有多少显示多少 tableView 包含 colloctionView 显示复杂的界面写法的更多相关文章

  1. IOS第八天(5:UITableViewController新浪微博, 计算行高)

    在 4 的 基础上重写 以下的方法 control #pragma mark - 代理方法 /** 计算单元格行高 */ - (CGFloat)tableView:(UITableView *)tab ...

  2. UITableView!别再用代码计算行高了(一)

    你还在用代码去计算行高吗?你不感觉那种方式很low吗?从今天起,试着做些改变吧! 别给我讲你喜欢写代码的感觉,你就是要用代码去计算行高,那我这篇文章不适合你. 在讲解复杂内容之前,还是先学习简单的内容 ...

  3. UITableView+FDTemplateLayoutCell计算行高显示<二>

    之前记录过一篇UITableView+FDTemplateLayoutCell计算行高不成功的博客... 传送门:http://www.cnblogs.com/pengsi/p/6571311.htm ...

  4. js动态创建的select2标签样式加载不上解决办法

    js动态创建的select2标签样式加载不上:调用select2的select2()函数来初始化一下: js抛出了Uncaught query function not defined for Sel ...

  5. jQuery:实现图片按需加载的方法,当要显示内容的高度超过了页面的高度,按需加载,根据滚动条的位置来判断页面显示的内容

    实现图片按需加载的方法,当要显示内容的高度超过了页面的高度,按需加载,根据滚动条的位置来判断页面显示的内容 这个类似于京东或淘宝页面,根绝页面的滚动,显示下面的内容 如下图所示,一开始并不是所有的图片 ...

  6. Spring BeanPostProcessor与动态加载数据源配置

    前言: 本文旨在介绍Spring动态配置数据源的方式,即对一个DataSource的配置诸如jdbcUrl,user,password,driverClass都通过运行时指定,而非由xml静态配置定死 ...

  7. iOS 根据字符串内容动态计算行高

    + (CGFloat)textHeightFromTextString:(NSString *)text width:(CGFloat)textWidth fontSize:(CGFloat)size ...

  8. java动态编译类文件并加载到内存中

    如果你想在动态编译并加载了class后,能够用hibernate的数据访问接口以面向对象的方式来操作该class类,请参考这篇博文-http://www.cnblogs.com/anai/p/4270 ...

  9. Spark2.x(六十一):在Spark2.4 Structured Streaming中Dataset是如何执行加载数据源的?

    本章主要讨论,在Spark2.4 Structured Streaming读取kafka数据源时,kafka的topic数据是如何被执行的过程进行分析. 以下边例子展开分析: SparkSession ...

随机推荐

  1. [CareerCup] 17.8 Contiguous Sequence with Largest Sum 连续子序列之和最大

    17.8 You are given an array of integers (both positive and negative). Find the contiguous sequence w ...

  2. HDU1892二维树状数组

    See you~ Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Su ...

  3. 谷歌浏览器安装adblock广告屏蔽插件

    访问不到google的应用商店的话,就从网上直接查找adblock for chrome.打开浏览器的开发者模式,下载解压后直接拖拽到浏览器即可.下载地址http://www.cr173.com/so ...

  4. websocket总结

    一.WebSocket简介 WebSocket  protocol是HTML5一种新的协议,WebSocket 是目前唯一真正实现全双工通信的服务器向客户端推送的互联网技术.WebSocket的出现使 ...

  5. php图片转为资源数据

    $file='C:\Users\feng\Desktop\images\banner.png';  //图片路径 $type=getimagesize($file);                 ...

  6. BizTalk开发系列(三) 单机环境下的BizTalk Server 2006 R2安装

    大部分的开发环境都是在单机环境下进行的,今天整理了一下BizTalk Server 2006 R2在单机环境下的安装步骤. 1. 软件需求 在独立服务器中完整安装BizTalk Server 2006 ...

  7. Python的全局变量

    应该尽量避免使用全局变量.不同的模块都可以自由的访问全局变量,可能会导致全局变量的不可预知性.对全局变量,如果程序员甲修改了_a的值,程序员乙同时也要使用_a,这时可能导致程序中的错误.这种错误是很难 ...

  8. VirtualMachine所支持的操作

    在JDK中com.sun.tools.attach.VirtualMachine提供了一些从外部进程attach到jvm上,并执行一些操作的功能.VirtualMachine的子类HotSpotVir ...

  9. 阿里云专有网络与弹性公网IP

    阿里云服务器经典网络和专有网络究竟有什么区别? 在用户提交订单购买阿里云ECS云服务器时,会面临怎样选择网络类型的烦恼,阿里云服务器定制购买时,网络类型里的经典网络和专有网络(VPC)是什么含义,该怎 ...

  10. 修改FreeBSD启动菜单停留时间

    原文:http://blog.chinaunix.net/uid-21782158-id-11006.html 分类: 两种情况: 一.启动时出现菜单的时候要停的10秒 方法: 编辑/boot/loa ...