collectionView的案例

  1. #import "ViewController.h"
  2. #import "CollectionViewCell.h"
  3. @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
  4. @property (weak, nonatomic) IBOutlet UICollectionView *collectionCell;
  5.  @end

 

  1. @implementation ViewController
  2. - (void)viewDidLoad
  3. {
  4.     [super viewDidLoad];
  5.     self.collectionCell.backgroundColor = [UIColor greenColor];
  6.     self.collectionCell.dataSource = self;
  7.     self.collectionCell.delegate = self;
  8. #pragma mark -- UICollectionViewDataSource
  9. //设置collectionCell的个数
  10. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  11. {
  12.     return 9;
  13. }
  14. //定义展示的Section的个数
  15. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  16. {
  17.     return 1;
  18. }
  19. //每个UICollectionView展示的内容
  20. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  21. {
  22.     //使用collectionCell进行注册,通过xib文件加载视图
  23.     [self.collectionCell registerNib:[UINib nibWithNibName:@"myCell" bundle:nil]
  24.           forCellWithReuseIdentifier:@"mycell"];
  25.     CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"mycell" forIndexPath:indexPath];
  26.     //图片名称
  27.     NSString *imageToLoad = [NSString stringWithFormat:@"%ld.jpg", indexPath.row];
  28.     //加载图片
  29.     cell.iamgeView.image = [UIImage imageNamed:imageToLoad];
  30.     //设置label文字
  31.     cell.labeiInfo.text = [NSString stringWithFormat:@"image%ld",indexPath.row];
  32.     return cell;
  33. }
  34. //设置每个各自的大小
  35. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  36. {
  37.     return CGSizeMake(170, 170);
  38. }
  39.  //定义每个UICollectionView 的 margin
  40. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  41. {
  42.     return UIEdgeInsetsMake(5, 5, 5, 5);
  43. }
  44.  //UICollectionView被选中时调用的方法
  45. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  46. {
  47.     UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
  48.     cell.backgroundColor = [UIColor whiteColor];
  49. }
  50. //返回这个UICollectionView是否可以被选择
  51. -(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
  52. {
  53.     return YES;
  54. }
  55. //隐藏状态栏
  56. -(BOOL)prefersStatusBarHidden
  57. {
  58.     return YES;
  59. }
  60. @end

程序运行结果:

 

全部评论

相关推荐

06-26 10:08
门头沟学院 C++
北京Golang实习,一个月4700,吃住都不报,公司位置在海淀。请问友友怎么看呢?如果要租房的话有什么建议吗
码农索隆:租房肯定是合租了,剩下的钱,差不多够正常吃饭了,看看能不能学到东西吧
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-29 17:30
找实习找着找着就要进入7月了,马上秋招也要开始了,找实习还有意义吗?
绝迹的星:有面就面, 没面上就当日薪4位数大佬免费培训, 面上了再考虑要不要实习
点赞 评论 收藏
分享
牛客38347925...:9,2学生暑期实习失利开始投小厂,给这群人整自信了
点赞 评论 收藏
分享
湫湫湫不会java:先投着吧,大概率找不到实习,没实习的时候再加个项目,然后把个人评价和荣誉奖项删了,赶紧成为八股战神吧,没实习没学历,秋招机会估计不多,把握机会。或者说秋招时间去冲实习,春招冲offer,但是压力会比较大
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务