2016-09-03 7 views
1

NSObjectコントローラにCollectionViewを追加するには? 私は各ViewControllerでアクセスしている1つのNSObjectコントローラを持っています。イメージビューのように既にいくつかのコンポーネントを追加しました。今、私はそれにCollectionViewを追加したいと思います。 AboutMe.hファイル内NSObjectコントローラのUICollectionViewにデリゲートを設定する方法

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

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

とAboutMe.mで

@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 
"AboutMe.h"

の#importファイル:次のように私は、コードを書かれています

私はViewControllerでこれにアクセスしています:

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

白い空白が表示されます。

ここで、私はDelegateをCollectionViewに設定できません。 これで私を助けてください。

答えて

0

AboutMeクラスの継承を変更しない限り、CollectionViewデリゲートは機能しません。

ですから、この行を更新してのUIViewControllerを継承する必要があります。

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

また、私は追加の提案になるだろう。このような方法で作業する場合:objMessageView有する例示的インスタンス化と操作が別の方法で配置することができるために

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

+0

など、それだけでコードを維持するようにしてください、それが作成に関連するとUIButtonを返しますこんにちはpedrouan、私はちょうどあなたの方法でそれをテストしたが、私のために働くことはありません、代理人は呼び出されません。私がセルフとしてデリゲートを設定していることに言及したいと思うもう一つのポイントは、私のために問題を引き起こすでしょうか? そして提案に感謝私は提案されたように私のコードを変更しました。 –

関連する問題