srx975546017 2015-11-12 08:46 采纳率: 0%
浏览 2606
已结题

同一界面下 两个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

  • 写回答

4条回答

  • 简单平凡之旅 2015-11-13 01:42
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题