3

カスタマイズするために UITableViewCell をサブクラス化しましたが、何かが足りないと思います: 1) 動作していない、2) 混乱していることがいくつかあります。.xib ファイルの外観をカスタマイズすると共に、backgroundView も変更しましたが、その部分は正常に動作しています。私が最も理解していない/最も混乱している部分は init メソッドなので、ここに投稿しました。それが正しいことが判明した場合は、原因である可能性のあるコードをさらに投稿できるように教えてください。

これは、私がカスタマイズした init メソッドです。私は「スタイル」のアイデアについて混乱していて、通常の UITableViewCell を別の backgroundView で返しているだけだと思います。つまり、.xib を参照したり、.backgroundView を自己から変更したりする以外に何かを行うものは何もありません。

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier wait: (float) wait fadeOut: (float) fadeOut fadeIn: (float) fadeIn playFor: (float) playFor
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        CueLoadingView* lview = [[CueLoadingView alloc] initWithFrame:CGRectMake(0, 0, 320, 53)];
        self.backgroundView = lview;

        [self setWait:wait]; // in turn edits the lview through the backgrounView pointer
        [self setFadeOut:fadeOut];
        [self setFadeIn:fadeIn];
        [self setPlayFor:playFor];
    }
    return self;
}

.xib といくつかのセッターとゲッターを除いて、これはセルの取得に関連するコードの唯一の実際の部分です。

追加情報:

1) これは、クラスにリンクされている私の .xib です。 ここに画像の説明を入力

2) これは、UITableView (デリゲート/ビュー コントローラー) を呼び出し/作成するコードです。

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

    CueTableCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[CueTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier wait:5.0 fadeOut:1.0 fadeIn:1.0 playFor:10.0];
        [cell updateBarAt:15];
    }

    return cell;
}
4

3 に答える 3

12

nib ファイルでカスタム テーブル ビュー セルを作成する最も簡単な方法 (iOS 5.0 以降で利用可能) はregisterNib:forCellReuseIdentifier:、テーブル ビュー コントローラーで使用することです。大きな利点は、dequeueReusableCellWithIdentifier:必要に応じて nib ファイルからセルを自動的にインスタンス化することです。その部分はもう必要ありif (cell == nil) ...ません。

追加viewDidLoadするテーブルビューコントローラーの

[self.tableView registerNib:[UINib nibWithNibName:@"CueTableCell" bundle:nil] forCellReuseIdentifier:@"CueTableCell"];

そして、cellForRowAtIndexPathあなたはただするだけです

CueTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CueTableCell"];
// setup cell
return cell;

nib ファイルからロードされたセルはinitWithCoder、 を使用してインスタンス化されます。必要に応じて、サブクラスでそれをオーバーライドできます。UI 要素を変更するには、オーバーライドする必要がありますawakeFromNib("super" を呼び出すことを忘れないでください)。

于 2013-03-23T20:03:28.750 に答える
1

代わりに .xib からセルをロードする必要があります。

if ( cell == nil ) {
    cell = [[NSBundle mainBundle] loadNibNamed:@"CellXIBName" owner:nil options:nil][0];
}

// set the cell's properties
于 2013-03-23T20:00:54.283 に答える
1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"CueTableCell";

    CueTableCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"CueTableCell XibName" owner:self options:nil];
        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
        cell = [array objectAtIndex:0];
    }

    return cell;
}
于 2013-03-23T20:01:26.960 に答える