ios- 同一界面下 两个collectionView 如何使用 求大神解答

问题描述

同一界面下 两个collectionView 如何使用 求大神解答

我想在一个controller下做两个不section 和cell 数不同的 collectionView 请大神指教
给跪了!!!

#import "ViewController.h"

#define Screen_Width [UIScreen mainScreen].bounds.size.width
//获取屏幕高度
#define Screen_Height [UIScreen mainScreen].bounds.size.height

#define left 1001
#define right 1002

@interface ViewController ()

@property (strong, nonatomic)UICollectionView *collectionViewLeft;
@property (strong, nonatomic)UICollectionView *collectionViewRight;

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    }
  • (void)viewWillAppear:(BOOL)animated
    {
    [super viewWillAppear:animated];
    [self initUI];
    }

-(void)initUI
{
UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
// [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[flowLayout setHeaderReferenceSize:CGSizeMake(Screen_Width/2, 50)];
self.collectionViewLeft=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, Screen_Width/2, Screen_Height-64) collectionViewLayout:flowLayout];
self.collectionViewLeft.dataSource=self;
self.collectionViewLeft.delegate=self;
self.collectionViewLeft.tag=left;
[self.collectionViewLeft setBackgroundColor:[UIColor blackColor]];
//注册Cell,必须要有
[self.collectionViewLeft registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
//注册headView,有headView必须有注册
[_collectionViewLeft registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
[self.view addSubview:self.collectionViewLeft];

self.collectionViewRight=[[UICollectionView alloc] initWithFrame:CGRectMake(Screen_Width/2, 20, Screen_Width/2, Screen_Height-20) collectionViewLayout:flowLayout];
self.collectionViewRight.dataSource=self;
self.collectionViewRight.delegate=self;
self.collectionViewRight.tag=right;
[self.collectionViewRight setBackgroundColor:[UIColor blackColor]];
//注册Cell,必须要有
[self.collectionViewRight registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
//注册headView,有headView必须有注册
[_collectionViewRight registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
[self.view addSubview:self.collectionViewRight];

}
#pragma mark -- UICollectionViewDataSource

//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if (collectionView.tag==left) {
return 3;
}else{
return 2;
}
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
if (collectionView.tag==left) {
return 5;
}else{
return 3;
}
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @"UICollectionViewCell";
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

cell.backgroundColor = [UIColor redColor];
for (id subView in cell.contentView.subviews) {
    [subView removeFromSuperview];
}
return cell;

}

#pragma mark --UICollectionViewDelegateFlowLayout

//定义每个Item 的大小

  • (CGSize)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    return CGSizeMake(100, 100);
    }

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
//方位
return UIEdgeInsetsMake(5, 5, 5, 5);
}

//设置headView的回调函数

  • (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
    {
    UICollectionReusableView *reusableview = nil;

    if (kind == UICollectionElementKindSectionHeader)
    {
    reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];

    reusableview.backgroundColor = [UIColor redColor];
    

    }
    UIView * view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,Screen_Width, 50)];
    view.backgroundColor=[UIColor orangeColor];
    [reusableview addSubview:view];
    return reusableview;
    }
    #pragma mark --UICollectionViewDelegate

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//临时改变个颜色,看好,只是临时改变的。如果要永久改变,可以先改数据源,然后在cellForItemAtIndexPath中控制。(和UITableView差不多吧!O(∩_∩)O~)
cell.backgroundColor = [UIColor greenColor];
NSLog(@"%d ",collectionView.tag);
NSLog(@"item======%ld",(long)indexPath.item);
NSLog(@"row=======%ld",(long)indexPath.row);
NSLog(@"section===%ld",(long)indexPath.section);
}

//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}

@end

解决方案

问题是什么呢 ?你贴个代码想表达什么?是没有实现还是 实现的结果 跟预想的不一样?

解决方案二:

两个collectionview,你可以给它们的tag赋值,数据源,代理,都传有一个collectionview,你依据它的tag返回你的指定的数据源和cell内容等等。

解决方案三:

两个collectionview,你可以给它们的tag赋值,数据源,代理,都传有一个collectionview,你依据它的tag返回你的指定的数据源和cell内容等等。

解决方案四:

一个控制器可以管理两个、多个collection View,判断一下

时间: 2024-08-03 15:43:29

ios- 同一界面下 两个collectionView 如何使用 求大神解答的相关文章

浏览器不知道怎么了,进不去网页,出现以下这个界面,这种情况怎么解决?求大神解答

问题描述 浏览器不知道怎么了,进不去网页,出现以下这个界面,这种情况怎么解决?求大神解答 400Bad Request 400 Bad Request nginx 解决方案 除了q107770540 说的这些,看一下DNS cache 解决方案二: 打开IE 在"Internet选项"里选择删除COOKIES 删除文件就可以了答3023案补充够详细的了.答3023案补充打开IE(你看网页的那个),然后点"工具"--Internet选项--Internet临时文件--

linux-Linux下安装Android Studio出错,求大神解答!

问题描述 Linux下安装Android Studio出错,求大神解答! JDK都配置好了,运行 zoegreen@zoegreen-Lenovo-IdeaPad-Y470:/opt/android-studio/bin$ java -version java version "1.7.0_51" Java(TM) SE Runtime Environment (build 1.7.0_51-b13) Java HotSpot(TM) Server VM (build 24.51-b0

android studio-Linux下安装Android Studio出错,求大神解答!

问题描述 Linux下安装Android Studio出错,求大神解答! JDK都配置好了,运行 zoegreen@zoegreen-Lenovo-IdeaPad-Y470:/opt/android-studio/bin$ java -version java version "1.7.0_51" Java(TM) SE Runtime Environment (build 1.7.0_51-b13) Java HotSpot(TM) Server VM (build 24.51-b0

界面更新 listview-Handler里面不能更新界面 包括List和Button的显示 求大神解答

问题描述 Handler里面不能更新界面 包括List和Button的显示 求大神解答 首先设置的是Activity的OnCrate方法 public class CreateActivity extends Activity{ protected static CharSequence text = "Waiting Range"; public static MobilocMaster mobilocMaster = new MobilocMaster(); public stat

请教下关于hibernate的 映射问题 求大神解答

问题描述 hibernate一对一双向关联A类外建关联B类用hql查询语句formAjoinfatchB得到A对象和A.getB对象但是hibernate为了给B对象setA对象会自动发出单独的sql语句有几个A对象就发几条这个怎么解决? 解决方案 解决方案二:inverse="true"双向关联一般都要这样配置让某一方来维护sql解决方案三:我在B类设置了mappedBy啊解决方案四:楼上说的差不多那意思解决方案五:自己顶下.....

求大神解答一下-求大神解答下这两道题目。。。。

问题描述 求大神解答下这两道题目.... 下面的题目是什么意思啊? 所有程序代码中状态显示定义到指定中类,还再进行本地判断 来源于DictionaryActivate.cs DictionaryActivate Activate = new DictionaryActivate(); 还有就是 if (Request.HttpMethod == "POST") { postapp(); } else if (Request.HttpMethod == "GET")

php编程-在mysqli->query()语句中遇到的两个问题,求大神解答下

问题描述 在mysqli->query()语句中遇到的两个问题,求大神解答下 <?php $mysqli=new mysqli("localhost", "root", "", "test"); if($mysqli->connect_error){ die( "connection failed".$mysqli->connect_error); }else{ echo "

ssl证书生成与配置问题,求大神解答下。

问题描述 ssl证书生成与配置问题,求大神解答下. 刚刚接触ssl 证书加密,只在自己的机器上试用了下,有两个问题不太懂,求解答下. 1,我在自己的机器上生成客户端.服务器证书,然后相互认证,配置tomcat.web.xml, 如果我在客户端在另一台机器上,我是不是把客户端证书拷贝到另一台机器上就可以正常访问了. 2,在1成立的基础上,如果有成千上万台客户端,证书都配置.拷贝完毕.突然服务器证书意外损坏,这时按照生成服务器证书的参数重新生成证书,能不能正常使用?如果不能,由于修改客户端证书工作量

ios-如何卸载 Xcode 上的 IOS 模拟器 求大神解答 谢谢

问题描述 如何卸载 Xcode 上的 IOS 模拟器 求大神解答 谢谢 如何卸载 Xcode 上的 IOS 模拟器 求大神解答 谢谢.网上真心找不到 方法啊 解决方案 把Xcode下的模拟器路径中的sdk删掉就行了,Contents/Developer/Platforms/iPhoneSimulator.platform/Developer 解决方案二: