UICollectionView 自定义使用

本文介绍如何使用UICollectionView展示水平滚动的自定义Cell,并详细展示了如何设置布局、注册Cell及填充数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面是头文件(
FirstViewController
.h)代码

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
{
  UICollectionView *collectionView_;
}

.m文件代码 其中我自定义了一个Cell名为MultipleCell.h

#import "MultipleCell.h"
@interface FirstViewController ()

@end

@implementation FirstViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    //  设置布局
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //UICollectionViewLayoutAttributes *flowLayout=[[UICollectionViewLayoutAttributes alloc]init];
    //每个cell的大小
    [flowLayout setItemSize:CGSizeMake(155,150)];
    
    //设置方向(向下还是向右)垂直Vertical  水平Horizontal
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    
    //设置距离边框的高度
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 0, 0, 0);//top、lef、bottom、right
    

  
  collectionView_  = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) collectionViewLayout:flowLayout];
  [self.view addSubview:collectionView_];
  collectionView_.backgroundColor = [UIColor redColor];
  collectionView_.delegate = self;
  collectionView_.dataSource = self;
  
  [collectionView_ registerClass:[MultipleCell class] forCellWithReuseIdentifier:@"simpleCell"];

  
	// Do any additional setup after loading the view.
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
  return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *cellIdentifier = @"simpleCell";
  MultipleCell *cell = (MultipleCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
  
  if(cell == nil)
  {
    //cell = [[[MultipleCell alloc] initWithFrame:CGRectMake(0, 0, 40, 40)] autorelease];
  }
  cell.imgView.image = [UIImage imageNamed:@"01.png"];
  cell.nameLabel.text = @"Weico";
  cell.detailLabel.text = @"许士彦";
  cell.markLabel.text =[NSString stringWithFormat:@"%d",indexPath.row];
//点击后背景图片
   cell.selectedBackgroundView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"01"]];
  
  return cell;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
  return 20;
}
自定义的cell.h文件

@property (nonatomic,retain)UIImageView *imgView;
@property (nonatomic,retain)UILabel *nameLabel;
@property (nonatomic,retain)UILabel *detailLabel;
@property (nonatomic,retain)UILabel *markLabel;
.m文件

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
      self.backgroundColor = [UIColor clearColor];
      imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];
      [self addSubview:imgView];
      
      
      nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, 90, 30)];
      nameLabel.backgroundColor = [UIColor clearColor];
      [self addSubview:nameLabel];
      
      
      detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 120, 60, 30)];
      detailLabel.backgroundColor = [UIColor clearColor];
      [self addSubview:detailLabel];
      
      
      markLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 120, 30, 30)];
      markLabel.backgroundColor = [UIColor clearColor];
      markLabel.textColor = [UIColor greenColor];
      [self addSubview:markLabel];
    }
    return self;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值