More Related Content
Similar to Uicollectionview
Similar to Uicollectionview (20)
Uicollectionview
- 28. // 指定された場所のセルを作るメソッド
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// セルを生成 or 再利用
LB8CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell"
forIndexPath:indexPath];
// 画像ファイル名
NSString *strName = [NSString stringWithFormat:@"%d.jpg", indexPath.row];
// 画像を貼り付け
cell.imageView.image = [UIImage imageNamed:strName];
// ラベルに番号を表示
cell.labelView.text = [NSString stringWithFormat:@"%d", indexPath.row + 1];
return cell;
}
コード書いていきます
28
∼ViewController.m
- 29. // iOS 5まで
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
余談…
29
// iOS 6から
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
再利用できなければ勝手にinit
TableViewCellの再利用にも若干の変化が
- 34. // セルのサイズを定義
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 画像ファイル名
NSString *strName = [NSString stringWithFormat:@"%d.jpg", indexPath.row];
// ファイル
UIImage *image = [UIImage imageNamed:strName];
// 画像サイズを定義
const CGFloat height = 65.0f;
const CGFloat width = height * image.size.width / image.size.height;
return CGSizeMake(width, height);
}
セルのサイズを画像依存に
34
∼ViewController.m