iOS开发仿电商类APP首页实例

2020-01-18 17:27:18于海丽

注意:

1、在主控制器中最好将automaticallyAdjustsScrollViewInsets属性设置为NO这个原因我在上一篇文章一行代码实现图片无限轮播器中有说明,如果在有NavigationBar的情况下如果不设置就会自动调整滚动视图的frame,这样会导致轮播器imageView显示错位。


- (void)viewDidLoad {
   [super viewDidLoad]; 

  //关闭自动调整滚动视图(不关闭图片轮播器会出现问题)
   self.automaticallyAdjustsScrollViewInsets = NO;
}

2、UICollectionView的UICollectionReusableView就相当于UITableView的headerView和footerView,要想给UICollectionView追加headerView就必须先注册,且追加的类型是UICollectionElementKindSectionHeader相对应的UICollectionElementKindSectionFooter就是追加footerView

实例化UICollectionView时的注册UICollectionReusableView代码


//注册UICollectionReusableView即headerView(切记要添加headerView一定要先注册)
    [_collectionView registerClass:[MYIHomeHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];

3、实现UICollectionView的数据源代理方法- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;


//添加headerView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  MYIHomeHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];

  //判断上面注册的UICollectionReusableView类型
  if (kind == UICollectionElementKindSectionHeader) {
    return headerView;
  }else {
    return nil;
  }
}