16

xib ファイルを使用する iOS 用のアプリを開発しています。

通常、ストーリーボードと を使用しますI don't know how to set up a UITableViewCell with xib files。UITableView を使用して xib ファイルを作成すると、いくつかの行を含むテーブルが表示されます。この行を編集して、配列に格納したものを書き込む必要があります。

xib ファイルを使用して UITableViewCell を設計するにはどうすればよいですか?

非常に単純なテーブルを作成する必要があります。タイトルを表示するには、セルの基本プリセットを使用します。私はデリゲートと DataSource をテーブル ビューのファイル所有者に接続する必要があることを知っており、ファイル所有者に UITableViewDelegate と UITableViewDataSource を入れました。

セルの内容を編集するにはどうすればよいですか? Web で UITableViewCell を使用して xib ファイルを作成するように指示するガイドを見つけて実行しましたが、これを操作する方法がわかりません

4

1 に答える 1

31

最初に、UITableViewCell から継承する customCell のクラスを作成する必要があります。ここで、customCell に必要なプロパティを追加します。この例では、cellImage と cellLabel を追加しました。

@property (nonatomic, strong) IBOutlet UILabel *cellLabel;
@property (nonatomic, strong) IBOutlet UIImageView *cellImageView;

その後、UILabel と UIImageView を CustomCell から Nib にリンクする必要があります。

以下を追加する必要があります。

- (void)viewDidLoad 
{
    ....
    [self.tableView registerNib:[UINib nibWithNibName:@"xibName" bundle:nil] forCellReuseIdentifier:CellIdentifier];
    .....
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomCellReuse";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    [cell.cellImageView setImage:[UIImage imageNamed:@"whatever"]];
    [cell.cellLabel setText = @"whatever"];
    return cell;
}
于 2013-10-16T09:39:22.803 に答える