iOS - UICollectionView

  UICollectionView可理解爲多列的UITableView,也繼承自UIScrollView,能夠進行滑動,能夠設治左右滑動或上下滑動。collectionViewLayout讓UICollectionView的佈局更靈活實用,且可編輯性更高。佈局

 

UICollectionViewFloeLayout (自定義成爲各類佈局的關鍵)spa

  minimumLineSpacing cell間距 這個是與你設置的滑動方向有關的,若是你設置上下滑動,那麼這就是cell上下兩個之間的最小間距,若是你設置成左右滑動,那麼這就是cell左右之間的最小間距代理

  minimumInteritemSpacing cell間距 這個與上面那個是相反的,若是你設置爲上下滑動,那麼這就是cell左右兩個之間最小間距,反正亦然code

  :這兩個屬性適合設定的滑動方向相關的,不是特指的行間距和列間距。minimumLineSpacing是指滑動方向上的兩個cell之間的距離,minimumInteritemSpacing是指與滑動方向垂直的方向上的兩個cell之間的距離。blog

  itemSize cell的大小。繼承

  estimatedItemSize cell預測的大小。若是 UICollectionView 的 layout 是一個 UICollectionViewFlowLayout,只須要將 layout.itemSize = ... 改爲 layout.estimatedItemSize = ...。collectionview 就會根據 cell 裏面的 autolayout 約束去肯定cell 的大小。(iOS8.0及之後)ci

  scrollDirection 滑動方向 默認是UICollectionViewScrollDirectionVertical 上下滑動。也能夠設置爲UICollectionViewScrollDirectionHorizontal左右滑動  it

  headerReferenceSize 頭部大小io

  footerReferenceSize 尾部大小class

  sectionInset 距離上下左右的距離

  sectionHeadersPinToVisibleBounds:設置是否當元素超出屏幕以後固定頭部視圖位置,默認NO(iOS9.0及之後)

  sectionFootersPinToVisibleBounds:設置是否當元素超出屏幕以後固定尾部視圖位置,默認NO(iOS9.0及之後)

 

UICollectionView的代理方法

//返回多少個分組
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每組有多少個cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 40;
}

//每一個位置顯示哪一個cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor yellowColor];
    return cell;
}

//點擊cell以後執行的操做
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

}

//返回YES則能夠移動cell的位置 
//這個須要給cell添加手勢,而後再在移動中交換cell位置 (下期會有介紹詳細使用 及demo)
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
相關文章
相關標籤/搜索