6

ある時点で UITableView を成長させる UIViewController があり、そのときに TableView インスタンス変数を初期化してビューに追加するだけですが、ビューに追加するセルのデキューを処理する方法がわかりません。再利用識別子が必要ですが、設定方法がわかりません。

このメソッド内で何をしますか?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"wot";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    return cell;
}
4

2 に答える 2

7

メソッドを使用するinitWithStyle:reuseIdentifier

  1. cell存在するかどうかを確認する
  2. そうでない場合は、初期化する必要があります。

コード

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath*)indexPath 
{
    static NSString *cellIdentifier = @"wot";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    if (!cell)
        cell = [[UITableViewCell alloc] initWithStyle: someStyle reuseIdentifier: cellIdentifier];

    return cell;
}
于 2013-04-02T19:11:54.947 に答える
0

再利用識別子を明示的に定義する必要はありません。cellForRowAtIndexPathメソッドでは、問題に含めた定義で十分に作業できます

参考までに

例えば

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyReuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:MyIdentifier]];
    }
    Region *region = [regions objectAtIndex:indexPath.section];
    TimeZoneWrapper *timeZoneWrapper = [region.timeZoneWrappers objectAtIndex:indexPath.row];
    cell.textLabel.text = timeZoneWrapper.localeName;
    return cell;
}
于 2013-04-02T19:13:44.360 に答える