static NSString *CellIdentifier = @"Cell";まず、UITableViewの「dequeueReusableCellWithIdentifier:」というメソッドを呼び出し、UITableViewCellを取得します。これは、待機中の再利用可能なセルをキューから取り外して取得する、ということをやっています。
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}もし、取り出したセルがnilなら(つまり、取り出せなかったら)、そのときは「まだ使い回して利用するセルができてない」ということなので、新しいUITableViewCellインスタンスを用意します。これは、allocしたのち、initWithStyle:というものでセルのスタイルを指定して初期化しています。reuseIdentifier:には、先ほどと同じIDを指定します。IDが違うと、再利用して取り出すときにうまく見つけられませんからね。NSString *msg = [NSString stringWithFormat:@"cell No,%i",indexPath.row];さて、これが新たに追加したものです。取り出した再利用可能なUITableViewCellは、まだ特に何も表示などは設定されていません。そこで、指定のNSIndexPathに表示するセルの設定などを行っておく必要があります。
[[cell textLabel] setText:msg];
※リストが表示されない場合
AddBlockなどの広告ブロックツールがONになっているとリストなどが表示されない場合があります。これらのツールをOFFにしてみてください。
※RootViewController.mで編集するメソッド
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return 5; // ★項目数
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:
CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
NSString *msg = [NSString stringWithFormat
:@"cell No,%i",indexPath.row]; // ★表示テキスト
[[cell textLabel] setText:msg]; // ★テキスト設定
return cell;
}
| << 前へ | 次へ >> |