How to set UILabel on UICollectionViewCell?

后端 未结 1 769
滥情空心
滥情空心 2021-01-07 11:08

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

相关标签:
1条回答
  • 2021-01-07 11:39

    try this it works for me.

    cell.h file

         #import <UIKit/UIKit.h>
    
         @interface CelebretiesCollectionViewCell : UICollectionViewCell
         {
             UIImageView *imgHairPhoto;
         }
         @property (nonatomic, retain) IBOutlet UILabel *titleLabel;
         @property (nonatomic, retain) IBOutlet UIImageView *imgHairPhoto;
    

    cell.m file

         @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
    

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

    enter image description here

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

    now use of collection view

            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.
           }
    
    0 讨论(0)
提交回复
热议问题