iOS-TableViewとCollectionViewの違い
Difference Between Ios Tableview
1.契約に従います。
UITableView: UITableViewDataSource,UITableViewDelegate
UIcollectionView: UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
2.セルを登録します。
Tableviewはセルを登録できません
collectionViewはセルを登録する必要があります。登録しないとクラッシュします
①UITableView:
use [tableView dequeueReusableCellWithIdentifier:]
セルの作成方法は、セルを登録しなくても実行できますが、セルが空の場合を考慮して処理する必要があります。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@'CellID'] if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@'CellID'] } cell.textLabel.text = @'system cell' return cell }
次のコードを使用してセルを作成する場合は、セルを登録する必要があります。そうしないと、クラッシュします。
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@'CellID' forIndexPath:indexPath]
注:セルが登録されている場合、-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathメソッドは、セルが空であることを処理する必要はありません。
②UICollectionView:
セルが空の場合を処理せずにセルを登録する必要があります。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@'CustomCollectionViewCell' forIndexPath:indexPath] return cell }
3、CollectionView固有のプロパティ:
UICollectionViewFlowLayoutオブジェクト
- セクションの左上、下、および右方向からの現在のCollectionViewのオフセットを制御するsectionInsetプロパティがあります
- 行間隔(minimumLineSpacing)と列間隔(minimumInteritemSpacing)のプロパティがあります(itemSizeはTableViewのrowHeightに似ていますが詳細には触れません)
開発中にsectionInsetを利用できます。
幅375の画面では、3つの項目を表示するために1行が必要で、列間隔は0です。設定方法は?
a> collectionViewの幅は300に設定できます
b>設定sectionInset = UIEdgeInsetsMake(0, 37.5, 0, 37.5)
4.要約:
セル作成時のバンドforIndexPath:
メソッドにはセルの登録が必要です
はすでにセルを登録しているので、セルが空であると判断する必要はありません。