0

ボタンがクリックされたときにテーブルビューコントローラーを表示するビューコントローラーがあります。すべてがテーブル ビュー デリゲートで正常に動作し、テーブル ビューは正常に表示されますが、ellForRowAtIndexPath: デリゲート メソッドでは、セルがインスタンス化されて返されますが、正しく表示されません。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"alrededor";

    alrededorCell *cell = [tableView 
                           dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[alrededorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


    NSDictionary *categoria = [[NSDictionary alloc] initWithDictionary: [_categoriasArray objectAtIndex:indexPath.row]];

    NSLog(@"categoria %@", categoria);

    cell.title.text = [categoria valueForKey:@"title"];

    return cell;

}

どうもありがとう

4

2 に答える 2

2

なぜこのようにセルを作成するのですか?

if (cell == nil) {
    cell = [[alrededorCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:CellIdentifier];
}

独自のカスタムセルの場合、なぜUITableViewCellStyleDefaultを使用するのですか?

于 2012-04-19T11:01:48.263 に答える
1

ロードしているセルが UITableViewCell のサブクラスであり、インターフェイス ビルダーを使用してセルを構築した場合、いくつかのことを行う必要があります。

nibファイルで、作成時にそこにあるビューを削除し、UITableViewCellを追加して、そのクラスをalrededorCellに変更します。ファイルの所有者ではなく、セル クラスを変更します。ボタン、ラベルなどをリンクしている場合。ファイルの所有者ではなく、必ずセルにリンクしてください。また、セルの uniqueIdentifier を alrededor に設定します。

cellForRowAtIndexPath 内

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"alrededor";

    alrededorCell *cell = [tableView 
                       dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *xib = [[NSBundle mainBundle] loadNibNamed:@"nibName" owner:nil options:nil];
        for (alrededorCell *view in xib) {
            if ([view isKindOfClass:[alrededorCell class]]) {
                cell = view;
            }
        }
    }


    NSDictionary *categoria = [[NSDictionary alloc] initWithDictionary: [_categoriasArray objectAtIndex:indexPath.row]];

    NSLog(@"categoria %@", categoria);

    cell.title.text = [categoria valueForKey:@"title"];

    return cell;

}
于 2012-04-19T12:52:08.380 に答える