您的位置:首页 > 移动开发 > IOS开发

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

2015-12-25 20:37 211 查看
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 {[superviewDidLoad];[selfsetUpCollection];// Do any additional setup after loading the view.}-(void)setUpCollection{self.dataMArr = [NSMutableArrayarray];for(NSInteger index =0;index<9; index++){UIImage *image = [UIImageimageNamed:[NSStringstringWithFormat:@"%ld",(long)index+1]];NSString *title = [NSStringstringWithFormat:@"{0,%ld}",(long)index+1];NSDictionary *dic =@{@"image": image,@"title":title};[self.dataMArraddObject:dic];}self.mkconllection.delegate =self;self.mkconllection.dataSource =self;}#pragma mark - Collection View Data Source-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{returnself.dataMArr.count;}-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{static NSString *collectionCellID =@"myCollectionCell";collectionCellmk *cell = (collectionCellmk *)[collectionViewdequeueReusableCellWithReuseIdentifier:collectionCellIDforIndexPath: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 一致staticNSString *collectionCellID = @"myCollectionCell";collectionCellmk *cell = (collectionCellmk *)[collectionViewdequeueReusableCellWithReuseIdentifier:collectionCellIDforIndexPath:indexPath];2 如果崩溃无法找issue ui 有可能是 连接太多出的问题
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: