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;
};
static NSString *collectionCellID = @"myCollectionCell";
collectionCellmk *cell = (collectionCellmk *)[collectionView dequeueReusableCellWithReuseIdentifier:collectionCellID forIndexPath:indexPath];
2 如果崩溃无法找issue ui 有可能是 连接太多出的问题