如何在NSObject控制器中将委托设置为UICollectionView

时间:2016-09-03 05:31:58

标签: objective-c iphone

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

在AboutMe.h文件中

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

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

并在AboutMe.m文件中

#import&#34; AboutMe.h&#34;

@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中访问它:

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

加载空白白色视图。

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

1 个答案:

答案 0 :(得分:0)

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

所以你需要更新这一行并继承自UIViewController:

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

我还会提出一个额外的建议。使用这样的方法时:

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

尝试仅保留与创建和返回UIButton相关的代码,因此例如使用objMessageView进行实例化和操作可以放在另一个方法中等。