如何在UICollectionViewCell上设置UILabel? [英] How to set UILabel on UICollectionViewCell?

查看:70
本文介绍了如何在UICollectionViewCell上设置UILabel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用自定义和标准UITableView单元格进行了数十次此操作.我所有的插座都已连接. UILabel是我的UICollectionViewCell在IB中的子视图.我的UICollectionViewCell对象继承了身份检查器中的适当类.

I've done this several dozen times with custom and standard UITableView cells. All my outlets are connected. The UILabel is a subview of my UICollectionViewCell in IB. My UICollectionViewCell object inherits the proper class in identity inspector.

如何在UICollectionViewCell上设置UILabel?

-(void)setCellName:(NSString *)cellName {
    self.cellLabel.text = cellName;
}

ViewController.m

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ADMCell *cell = 
     (ADMCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"ADMCell" 
                                                          forIndexPath:indexPath];
    [cell setCellName:[NSString stringWithFormat:@"%d",[indexPath row]]];

    return cell;
}

cell.debugDescription的输出:

Output of cell.debugDescription:

2013-05-15 22:05:40.191 ProductName[857:c07] cell.debugDescription: 
   <ADMCell: 0xb35c890; baseClass = UICollectionViewCell; 
   frame = (245 266; 70 80); layer = <CALayer: 0xb35c780>>`

推荐答案

尝试一下对我有用.

cell.h文件

     #import <UIKit/UIKit.h>

     @interface CelebretiesCollectionViewCell : UICollectionViewCell
     {
         UIImageView *imgHairPhoto;
     }
     @property (nonatomic, retain) IBOutlet UILabel *titleLabel;
     @property (nonatomic, retain) IBOutlet UIImageView *imgHairPhoto;

cell.m文件

     @synthesize titleLabel,imgHairPhoto;

     - (id)initWithFrame:(CGRect)frame
     {
         self = [super initWithFrame:frame];
         if (self) {

             // Initialization code
             NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CelebretiesCollectionViewCell" owner:self options:nil];

             if ([arrayOfViews count] < 1) {
                 return nil;
             }

             if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
                 return nil;
             }

             self = [arrayOfViews objectAtIndex:0];

         }
         return self;
     }
     @end

//在xib中获取collectionviewCell并在CollectionViewXib上放置插座.

// Take collectionviewCell in xib and make outlet on CollectionViewXib.

/////////////////////////////////////

/////////////////////////////////////

现在使用收藏夹视图

        viewdidload method

          [self.YourCollectionViewObj registerClass:[CelebretiesCollectionViewCell class] forCellWithReuseIdentifier:@"celebretiesCollectionViewCell"];


       //Datasource and delegate method
       - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
                   return [arrFavorites count];///Your array count
       }
       - (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
           return 1;
       }
       - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

           static NSString *CellIdentifier = @"celebretiesCollectionViewCell";
           CelebretiesCollectionViewCell *cell;
           cell = (CelebretiesCollectionViewCell *)[YourCollectionViewObj dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
             cell.frame=CGRectMake(0, 0, 150, 120);// set frame as per xib

             cell.imgHairPhoto.image=[UIImage imageWithData:[NSData dataWithContentsOfFile:strPath]];
               cell.titleLabel.text = shrObj.strCelebrityName;
               return cell;
       }

       - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:        (UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
           return UIEdgeInsetsMake(-10, 0, 0, 0); //set as per your requirements.
       }

这篇关于如何在UICollectionViewCell上设置UILabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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