当前位置:   article > 正文

iOS CollectionView简单使用_ios colectionview的用法

ios colectionview的用法

UICollectionView的使用跟UITableView的使用相似,必须实现UICollectionViewDataSource,UICollectionViewDelegate,

UICollectionViewDelegateFlowLayout这三个协议。

首先创建collectionView:


  1. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  2. self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100 * ((self.dataArr.count - 1)/2 + 1)) collectionViewLayout:flowLayout];
  3. self.collectionView.dataSource = self;
  4. self.collectionView.delegate = self;
  5. self.collectionView.backgroundColor = [UIColor clearColor];
  6. [self.view addSubview:self.collectionView];
  7. [self.collectionView registerClass:[PrefectureCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];

然后列举一些经常使用的方法:

  1. #pragma mark -UICollectionView delegate
  2. /**
  3. * 有多少个分区
  4. */
  5. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  6. {
  7. return 1;
  8. }

  1. /**
  2. * 有多少个cell
  3. */
  4. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  5. {
  6. return _dataArr.count;
  7. }

  1. /**
  2. * 相当于单元格
  3. */
  4. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  5. {
  6. PrefectureCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
  7. NSDictionary *dit = [_dataArr objectAtIndex:indexPath.row];
  8. cell.imageV.backgroundColor = [UIColor cyanColor];
  9. cell.titleLabel.text = [dit objectForKey:@"name"];
  10. cell.detailLabel.text = [dit objectForKey:@"description"];
  11. return cell;
  12. }

  1. //每个cell尺寸
  2. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  3. {
  4. return CGSizeMake(self.view.frame.size.width/2.0, 100);
  5. }

  1. //每个cell间距
  2. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  3. {
  4. return UIEdgeInsetsMake(0, 0, 0, 0);
  5. }

  1. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  2. {
  3. return 0;
  4. }
  5. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;
  6. {
  7. return 0;
  8. }

  1. //UICollectionView被选中时调用的方法
  2. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  3. {
  4. UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
  5. cell.backgroundColor = [UIColor whiteColor];
  6. }
  7. //返回这个UICollectionView是否可以被选择
  8. -(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
  9. {
  10. return YES;
  11. }


自定义 PrefectureCollectionViewCell代码:

  1. - (id)initWithFrame:(CGRect)frame
  2. {
  3. self = [super initWithFrame:frame];
  4. if (self) {
  5. _imageV = [[UIImageView alloc] init];
  6. _imageV.frame = CGRectMake(10, (frame.size.height - 60.0)/2.0, 60, 60);
  7. _imageV.backgroundColor = [UIColor clearColor];
  8. [self.contentView addSubview:_imageV];
  9. _titleLabel = [[UILabel alloc] init];
  10. _titleLabel.frame = CGRectMake(_imageV.frame.origin.x + _imageV.frame.size.width + 5, _imageV.frame.origin.y + _imageV.frame.size.height - 45, frame.size.width - 70 - 5, 20.0);
  11. _titleLabel.backgroundColor = [UIColor clearColor];
  12. _titleLabel.textAlignment = NSTextAlignmentLeft;
  13. _titleLabel.font = [UIFont systemFontOfSize:17.0];
  14. _titleLabel.textColor = [UIColor colorWithWhite:0.2 alpha:1.0];
  15. [self.contentView addSubview:_titleLabel];
  16. _detailLabel = [[UILabel alloc] init];
  17. _detailLabel.frame = CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.origin.y + _titleLabel.frame.size.height, _titleLabel.frame.size.width, 20.0);
  18. _detailLabel.backgroundColor = [UIColor clearColor];
  19. _detailLabel.textAlignment = NSTextAlignmentLeft;
  20. _detailLabel.font = [UIFont systemFontOfSize:12.0];
  21. _detailLabel.textColor = [UIColor blackColor];
  22. [self.contentView addSubview:_detailLabel];
  23. }
  24. return self;
  25. }

  1. - (void)drawRect:(CGRect)rect {
  2. [super drawRect:rect];
  3. CGContextRef context = UIGraphicsGetCurrentContext();
  4. CGContextSaveGState(context);
  5. CGContextSetLineCap(context, kCGLineCapSquare);
  6. CGContextSetStrokeColorWithColor(context, [UIColor clearColor].CGColor);
  7. CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
  8. CGContextSetLineWidth(context, 1.0);
  9. CGContextMoveToPoint(context, 0, 0);
  10. CGContextAddLineToPoint(context, rect.size.width, 0);
  11. CGContextAddLineToPoint(context, rect.size.width, rect.size.height);
  12. CGContextAddLineToPoint(context, 0, rect.size.height);
  13. CGContextMoveToPoint(context,0, 0);
  14. CGContextClosePath(context);
  15. CGContextDrawPath(context, kCGPathFillStroke); //根据坐标绘制路径
  16. CGContextSetLineWidth(context, 0.6);
  17. CGContextSetStrokeColorWithColor(context,[UIColor lightGrayColor].CGColor);
  18. CGContextBeginPath(context);
  19. CGContextMoveToPoint(context, self.frame.size.width-1, 0);
  20. CGContextAddLineToPoint(context,self.frame.size.width -1, self.frame.size.height);
  21. CGContextAddLineToPoint(context, 0, self.frame.size.height);
  22. CGContextStrokePath(context);
  23. }

数据:

  1. self.dataArr = @[@{@"module":@"news",
  2. @"name":@"惠农快讯",
  3. @"catid": @"29",
  4. @"description":@"行业资讯快人一步",
  5. @"icon":@"http://ynb.114nz.com/Public/Appimg/news.png"},
  6. @{@"module":@"news",
  7. @"name":@"农情气象",
  8. @"catid":@"33",
  9. @"description":@"刮风下雨提前告知",
  10. @"icon":@"http://ynb.114nz.com/Public/Appimg/news_weather.png"
  11. },
  12. @{
  13. @"module":@"event",
  14. @"name":@"活动专区",
  15. @"catid":@"1621,1623",
  16. @"description":@"热门活动报名抢座",
  17. @"icon":@"http://ynb.114nz.com/Public/Appimg/event.png"
  18. },
  19. @{
  20. @"module":@"help",
  21. @"name":@"供求发布",
  22. @"catid":@"1089",
  23. @"description":@"供求信息互帮互助",
  24. @"icon":@"http://ynb.114nz.com/Public/Appimg/help.png"
  25. },
  26. @{
  27. @"module":@"tech",
  28. @"name":@"栽培技术",
  29. @"catid":@"1549",
  30. @"description":@" 详细的作物栽培技术",
  31. @"icon":@"http://ynb.114nz.com/Public/Appimg/tech_skill.png"
  32. },
  33. @{
  34. @"module":@"tech",
  35. @"name":@"解决方案",
  36. @"catid":@"1622",
  37. @"description":@"海量解决方案等你来",
  38. @"icon":@"http://ynb.114nz.com/Public/Appimg/tech_solve.png"
  39. },
  40. @{
  41. @"module":@"tech",
  42. @"name":@"试验示范",
  43. @"catid":@"1551,1552",
  44. @"description":@"您手中的试验田",
  45. @"icon":@"http://ynb.114nz.com/Public/Appimg/tech_testpro.png"
  46. }];


效果图:




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/669547
推荐阅读
相关标签
  

闽ICP备14008679号