IOS 开发小记 ————————collection view 总结


1 正常新建项目 

2  拖一个collection 到 controller 中

3  修改其中默认的一个cellview    加入 imageview 和 label    

然后新建一个viewcontroller 和 cellview 对接   ,在storyboard 把ID 设为 对应的viewcontroller 的名字 

4  使用collection 的controller  需要   . h中 实现两个接口 

#import <UIKit/UIKit.h>

@interface Test1ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>

@property (strong, nonatomic)NSMutableArray *dataMArr;
@property (weak, nonatomic) IBOutlet UICollectionView *mkconllection;

@end

5  对应的实现  :  

- (void)viewDidLoad {

    [super viewDidLoad];

    [self setUpCollection];

    // Do any additional setup after loading the view.

}

-(void)setUpCollection{

    self.dataMArr = [NSMutableArray array];

    for(NSInteger index = 0;index<9; index++){

        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",(long)index+1]];

        NSString *title = [NSString stringWithFormat:@"{0,%ld}",(long)index+1];

        NSDictionary *dic = @{@"image": image, @"title":title};

        [self.dataMArr addObject:dic];

    }

    self.mkconllection.delegate = self;

    self.mkconllection.dataSource = self;

    

    

}

#pragma mark - Collection View Data Source

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.dataMArr.count;

}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    

    static NSString *collectionCellID = @"myCollectionCell";

    collectionCellmk *cell = (collectionCellmk *)[collectionView dequeueReusableCellWithReuseIdentifier:collectionCellID forIndexPath:indexPath];

    

    NSDictionary *dic    = self.dataMArr[indexPath.row];

    UIImage *image       = dic[@"image"];

    NSString *title      = dic[@"title"];

    

    cell.CellImage.image = image;

    cell.celllabel.text = title;

    

    return cell;

};










注意事项
1 cell 的 identifier 需要手动加入 myCollectionCell 而且需要和 实现函数中使用的 id 一致

    static NSString *collectionCellID = @"myCollectionCell";

    collectionCellmk *cell = (collectionCellmk *)[collectionView dequeueReusableCellWithReuseIdentifier:collectionCellID forIndexPath:indexPath];


2 如果崩溃无法找issue ui 有可能是 连接太多出的问题




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值