如何在NSObject控制器中将委托设置为UICollectionView [英] How to set delegate to UICollectionView in NSObject controller

查看:53
本文介绍了如何在NSObject控制器中将委托设置为UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在NSObject控制器中添加CollectionView? 我在每个ViewController上都有一个NSObject控制器.我已经在上面添加了一些组件,例如图像视图.现在,我想在上面添加CollectionView.我编写了如下代码:

How to add CollectionView in NSObject controller? I have one NSObject controller which i am accessing on each ViewController. I already added some component on it like image view. Now I want to add CollectionView on it. I have written code as follows:

在AboutMe.h文件中

in AboutMe.h file

@interface AboutMe : NSObject<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

-(UIButton *) showAboutMeView:(UIViewController *)id;
@end

以及AboutMe.m文件中

#import"AboutMe.h"

@implementation AboutMe {

    UIView   *objMessageView;
    UICollectionView *_collectionView    
    NSMutableArray *arrstrProfileImage;
    NSMutableArray *arrstrUsersNameEn;
    NSMutableArray *arrstrUsersNameMr;
}

-(UIButton *) showAboutMeView:(UIViewController *)id {

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenX = screenRect.origin.x;
    CGFloat screenY = screenRect.origin.y;
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;

    arrstrProfileImage = [[NSMutableArray alloc] initWithObjects:@"male.png",@"female.png",nil];

    arrstrUsersNameEn = [[NSMutableArray alloc] initWithObjects:@"Mr. (Corporator)",@"Mrs.(Corporator)", nil];

    objMessageView = [[UIView alloc] initWithFrame:CGRectMake(screenX, screenY, screenWidth,screenHeight-64)];
    objMessageView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.8f];
    [id.view addSubview:objMessageView];

    UIImageView *objUIImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, screenWidth, 150)];
    objUIImageView.image = [UIImage imageNamed:@"abc.png"];
    objUIImageView.contentMode = UIViewContentModeScaleAspectFit;
    [objMessageView addSubview:objUIImageView];

    UIButton *objUIButtonOk  = [[UIButton alloc] initWithFrame:CGRectMake(screenX, screenHeight-120, screenWidth, 50)];
    [objUIButtonOk setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    objUIButtonOk.titleLabel.font = [UIFont boldSystemFontOfSize:25];
    [objUIButtonOk setTitle:@"OK" forState:UIControlStateNormal];
    [objMessageView addSubview:objUIButtonOk];

    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
    _collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(objMessageView.frame.origin.x+10, objUIImageView.frame.origin.y+objUIImageView.frame.size.height+5, objMessageView.frame.size.width-20, objUIButtonOk.frame.origin.y-(objUIImageView.frame.origin.y+objUIImageView.frame.size.height+25)) collectionViewLayout:layout];
    [_collectionView setDataSource:self];
    [_collectionView setDelegate:self];
    _collectionView.showsVerticalScrollIndicator = NO;
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
    [_collectionView setBackgroundColor:[UIColor gray]];

    [objMessageView addSubview:_collectionView];

    return objUIButtonOk;
}

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

    return arrstrProfileImage.count;
}

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

    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    UIImageView          *objUIImageView;
    UILabel              *objUILabel;

    for (UILabel *lbl in cell.contentView.subviews)
    {
        if ([lbl isKindOfClass:[UILabel class]])
        {
            [lbl removeFromSuperview];
        }
    }
    for (UIImageView *img in cell.contentView.subviews)
    {
        if ([img isKindOfClass:[UIImageView class]])
        {
            [img removeFromSuperview];
        }
    }
    cell.backgroundColor=[UIColor lightGrayColor];
    cell.layer.cornerRadius = 3;
    cell.layer.masksToBounds = YES;

    objUIImageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.frame.size.width/2-50, cell.contentView.frame.origin.y+25, 100, 100)];
    objUIImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[arrstrProfileImage objectAtIndex:indexPath.row]]];
    objUIImageView.contentMode = UIViewContentModeScaleAspectFit;
    [cell.contentView addSubview:objUIImageView];

    objUILabel = [[UILabel alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x+5, objUIImageView.frame.size.height+20, cell.contentView.frame.size.width-10, cell.contentView.frame.size.height - (objUIImageView.frame.size.height+20))];
    objUILabel.backgroundColor  = [UIColor clearColor];
    objUILabel.textAlignment    = NSTextAlignmentCenter;
    objUILabel.numberOfLines    = 0;
    objUILabel.lineBreakMode    = NSLineBreakByWordWrapping;
    objUILabel.font             = [UIFont boldSystemFontOfSize:16];
    objUILabel.textColor        = [UIColor blackColor];

    if ([[NSString stringWithFormat:@"%@",[UserDefault objectForKey:@"lang"]] isEqualToString:@"M"]) {
        objUILabel.text = [arrstrUsersNameMr objectAtIndex:indexPath.row];
    }else{
        objUILabel.text = [arrstrUsersNameEn objectAtIndex:indexPath.row];
    }
    [cell.contentView addSubview:objUILabel];

    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    return CGSizeMake(objMessageView.frame.size.width/2 -15,objMessageView.frame.size.width/2 -15);
}
@end

我在ViewController中以以下方式访问它:

I am accessing this in ViewController as:

AboutMe *objAboutMe = [[AboutMe alloc] init];
UIButton *objBtn = [objAboutMe showAboutMeView:self];
[objBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

加载空白的白视图.

这里的问题是我无法将Delegate设置为CollectionView. 请帮助我.

Here Problem is I am not able to set Delegate to CollectionView. Please Help me in this.

推荐答案

除非更改AboutMe类的继承,否则您是CollectionView委托将无法工作.

You're CollectionView delegate won't work unless you change inheritation of your AboutMe class.

因此,您需要更新此行并从UIViewController继承:

So you need to update this line and inherit from UIViewController:

@interface AboutMe : UICollectionViewController <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

我还要提出一个额外的建议.当使用这样的方法时:

Also I would make an additional suggestion. When working with method like this:

-(UIButton *) showAboutMeView:(UIViewController *)id {

尝试仅保留与创建和返回UIButton有关的代码,例如,可以将objMessageView的实例化和操作放置在其他方法中,等等.

Try to mantain only that code, that is related to creation and returning UIButton, so for example instantiation and operations with objMessageView could be placed in another method, etc.

这篇关于如何在NSObject控制器中将委托设置为UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆