2016-09-03 24 views
1

如何在NSObject控制器中添加CollectionView? 我有一個NSObject控制器,我正在訪問每個ViewController。我已經在圖像視圖中添加了一些組件。現在我想添加CollectionView。我寫的代碼如下:如何在NSObject控制器中設置委託給UICollectionView

在AboutMe.h文件

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

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

和AboutMe.m文件

#進口 「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中訪問這個爲:

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

空白視圖被加載。

這裏問題是我無法將代表設置爲CollectionView。 請幫我這個。

回答

0

你是CollectionView委託不會工作,除非你改變你的AboutMe類的繼承。

所以,你需要更新此行,從UIViewController的繼承:

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

而且我會做一個額外的建議。當使用方法是這樣工作的:

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

嘗試保持唯一的代碼,也就是涉及到創建並返回的UIButton,因此,例如實例化和操作與objMessageView可以被放置在另一個方法等

+0

你好,我只是用你的方式來測試它,但它不適用於我,沒有委託人被調用。還有一點我想提到,我將代表設置爲Self,這會對我造成問題嗎? 並感謝您的建議,我改變了我的代碼建議。 –