UITableView 和 UICollectionView的設計思路:佈局
1.UITableView 的佈局由TableView和UItableViewDelegate完成.spa
2.UICollectionView佈局樣式由UICollectionViewFlowLayout和UICollectionViewDelegate完成.設計
數據源:代理
1.UITableView由UITableViewDataSource提供.code
2.UICollectionView由UICollectionViewDataSource提供.blog
佈局樣式:繼承
1.UITableView是多列單行.ci
2.UICollectionView是多行多列.it
1.UITableViewCell上自帶imageView.textLable.detailLabel.io
2.UICollectionCell自帶了contentView,contentView.
1.UItableViewCell建立時直接使用,後來使用了重用機制+註冊.
2.UICollectionViewCell只有註冊.
共同點:兩者都是繼承於UIScrollView,夠能夠滾.
1 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];//初始化一個佈局類CollectionView 2 layout.itemSize = CGSizeMake(150, 200); //設置每一個Item大小 3 layout.sectionInset = UIEdgeInsetsMake(20, 5, 10, 5); //設置每一個item之間的間隙,上右下左 4 layout.minimumLineSpacing = 15; //最小行間距 5 UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];//初始化一個CollectionView,並使用layout初始化 6 7 collectionView.delegate = self;//設置代理和dataSource 8 collectionView.dataSource = self; 9 [self.view addSubview:collectionView]; 10 [collectionView release]; 11 [layout release]; 12 13 // 設置頁眉的大小 14 layout.headerReferenceSize = CGSizeMake(320,40); 15 layout.footerReferenceSize = CGSizeMake(320, 20); 16 17 // 註冊 Cell 18 [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCollectionCell]; 19 20 // 註冊 頁眉(header) 21 [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kElementHeadView]; 22 // 頁腳(footer) 23 [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kElementFootView];
代理UICollectionViewDataSource的協議中必須實現的兩個方法:
// 返回每一個分區裏 item 的個數 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { } //重用 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
}
1 // 點擊 item 觸發的方法 2 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 3 4 } 5 // 返回每一個 item 的大小 6 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 7 8 } 9 // 返回每一個分區的縮進量 10 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { 11 12 } 13 14 // 返回行間距 15 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { 16 17 } 18 // 返回 item 的間距 19 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { 20 21 } 22 23 // 返回頁眉的 size 24 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { 25 26 } 27 28 // 返回頁腳的 size 29 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { 30 31 }