9

私のアプリでは、ストーリーボードを使用しています。しかしUITableView、オブジェクト ライブラリからドラッグするのではなく、プログラムで作成しました。そして今、プログラムで作成された のセルをカスタマイズしたいと思いUITableViewます。UITableViewCellプログラムでストーリーボードを作成する例を提供してくれる人はいますか?

4

2 に答える 2

8

セルのレイアウトと構築を に入れることは避けたいと思いますcellForRowAtIndexPath

プログラムでカスタム セルを作成するには、まずUITableViewCellサブクラスを作成する必要があります。

labels、などを追加します。imageViewsのサブビューとして を追加しcell.contentViewます。

プログラム的に

すなわち

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 21)];
        [self.contentView addSubview:_label];
    }
    return self;
}

セルのレイアウトを行いたい場合は、MyCellクラスで行うことができます...

- (void)layoutSubViews
{
    [super layoutSubviews];
    // layout stuff relative to the size of the cell.
}

次にtableViewController、セルクラスを登録する必要があります...

viewDidLoad...

[self.tableView registerClass:[MyCell class] forCellReuseIdentifier:@"MyCellIdentifier"];

インターフェイスビルダー付き

カスタム サブクラスを作成しますが、同じ名前の xib ファイルも作成します。次に、xib ファイルで、セルの初期化でアウトレットを作成する代わりに、アウトレットを接続できます。(このようにすると、とにかく init は呼び出されません)。

他に必要な唯一の変更はviewDidLoad、クラスではなくセルの nib を登録する必要があることです。

このような...

UINib *cellNib = [UINib nibWithNibName:@"MyCell" bundle:nil];
[self.tableView registerNib:cellNib forCellReuseIdentifier:@"MyCellIdentifier"];

その後、他のすべては同じように機能します。

セルの使用

サブクラスを作成したセルを使用するには...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCellIdentifier"];

    [self configureCustomCell:(MyCell*)cell atIndexPath:indexPath];

    return cell;
}

- (void)configureCustomCell:(MyCell*)cell atIndexPath:(NSIndexPath *)indexPath
{
    // do all you logic of getting any info from arrays etc in here.

    cell.label.text = @"Blah".
}

まとめ

このようにすることは、テーブルビューコントローラーがセルに物を入れることにのみ関心があることを意味します。セルを構築するためのすべてのロジックを入れると、すべてが本当に面倒になります。

また、さまざまな UI 要素を保存および取得するために、さまざまなタグの負荷に対処する必要がないことも意味します。

于 2013-03-20T08:28:47.603 に答える
0

2つのオプションについて説明します。InterfaceBuilder(より単純)を使用してセルを追加する方法と、プログラムでセルを追加する方法です。

InterfaceBuilderを使用する

Interface Builderでセルを作成し、カスタムコンテンツでサブビューを追加したら、属性インスペクターを開き、カスタムスタイルを選択して、[識別子の再利用]テキストフィールドに一意の識別子(「anIdentifier」など)を入力します。次に、プログラムでアクセスするセルフィールドを選択し、各フィールドに一意のタグ番号を設定します( [表示]セクションの下にあります)。

次に、データソースコードで次のメソッドを実装します。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"anIdentifier"];

    UILabel *label;

    label = (UILabel *)[cell viewWithTag:1]; // Set a constant for this so your fellow developers understand this number
    label.text = @"This is a test";

    return cell;
}

プログラムで

プログラムでセルを作成する場合、コードは次のようになります。

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

    static NSString *CellIdentifier = @"anIdentifier";

    UILabel *aLabel;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]];

        aLabel = [[[UILabel alloc] initWithFrame:...]];
        aLabel.tag = 1; // Set a constant for this
        aLabel.font = [UIFont systemFontOfSize:14.0];
        aLabel.textAlignment = UITextAlignmentRight;
        aLabel.textColor = [UIColor blackColor];
        aLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
        [cell.contentView addSubview:mainLabel];
    } else {
        aLabel = (UILabel *)[cell.contentView viewWithTag:1];
    }

    aLabel.text = @"This is a test";

    return cell;
}

AppleのWebサイトに詳細があります:http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html

于 2013-03-20T07:51:38.280 に答える