【问题标题】:How to set UILabel on UICollectionViewCell?如何在 UICollectionViewCell 上设置 UILabel?
【发布时间】:2013-05-15 21:29:47
【问题描述】:

我已经用自定义和标准UITableView 单元完成了几十次。我所有的插座都已连接。 UILabel 是我在 IB 中的 UICollectionViewCell 的子视图。我的UICollectionViewCell 对象继承了身份检查器中的正确类。

如何在UICollectionViewCell 上设置UILabel

MyCell.m

-(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 的输出:

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

【问题讨论】:

  • 您是否将 IB 中的单元格设置为具有正确的标识符“ADMCell”以便正确出列?
  • 你可以在分配标题后使用 NSLog("%@",cell.debugDescription) 吗?以防万一 cell 为零...并确认 @"AMDCell" 是唯一注册的。
  • 那你有什么问题? self.cellLabel 是零吗?细胞是错误的细胞类型吗?如果您不告诉我们,我们不知道出了什么问题。
  • Dan Fairaizl,标识符设置为 ADMCell。亚历克斯,我已经包含了上面的输出。很抱歉缺少细节。 @Caleb setCellName: 从不在单元格上设置标签。 Check out this screenshot.
  • mmmm... 好的.. 如果您在 -cellForItemAtIndexPath:(NSIndexPath *)indexPath 中设置断点,并检查 cell.cellLabel,它是否包含预期的文本?还有文字标签,以防万一,前面有什么东西会混淆视图?

标签: iphone ios uicollectionview uicollectionviewcell


【解决方案1】:

试试这个对我有用。

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上做outlet。

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

现在使用集合视图

        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.
       }

【讨论】:

  • 旅行。当我有时间的时候我会试试这个。我认为这种方法应该仍然有效,因为我正在使用故事板。故事板上的单元格应该简单地从类继承,例如 CelebretiesCollectionViewCell 或 ADMCell。对吗?
  • 是的,使用您的单元格名称而不是 CelebretiesCollectionViewCell
  • 这成功了。有趣的是,这与自定义 UITableViewCell 有多么相似,我应该先尝试一下。谢谢你,先生!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
  • 2019-03-23
  • 2018-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多