2

TableView xibを使用していますが、すべてのデリゲートとデータソースが正常に実行されているようです。を設定self.textLabel.textすると、テーブルビューの一般的な数が正しく表示されますが、カスタムTableViewCellを表示する必要があります。

tableviewcellだけを含むHistoryCell.xibを作成しました。UITableViewCellクラス「HistoryCell.h/HistoryCell.m」を作成しました。これは、HistoryCell.xibのファイル所有者として設定されています。UILabelsをHistoryCell.hに接続しました UILabel statusLabel UILabel nameLabel UILabel timeLabel

私のメインのViewControllerクラスではcellForRowAtIndexPath

入れています

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

static NSString *CellIdentifier = @"historyCellType";
HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[HistoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.nameLabel.text = @"Donald Duck";

return cell;

}

私は何が間違っているのですか?

ありがとう!

4

4 に答える 4

8

次のようにペン先を登録する必要があります(おそらくviewDidLoadで):

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

次に、cellForRowAtIndexPathメソッドで、if cell==nil句が不要な新しい方法を使用します。

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

HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"historyCellType" forIndexPath:indexPath];

cell.nameLabel.text = @"Donald Duck";

return cell;

}

于 2013-03-21T21:21:43.170 に答える
4

実際のXIBデータを逆シリアル化する必要があります。

HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *cellnib = [[NSBundle mainBundle] loadNibNamed:@"HistoryXIBName" owner:self options:nil];
    cell = (HistoryCell *)[cellnib objectAtIndex:0];
}

それ以外の場合は、「デフォルト」セルを使用しているだけです。

編集:また、ストーリーボードを使用している場合、動的セルプロトタイプを使用すると、NIBファイルからセルを作成する必要がなくなります(オプションの場合)。

2番目の編集: これも試すことができます(これは、ARCを使用していることを前提としています):

HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    HistoryCell *aNewCell = [[HistoryCell alloc] initWithNibName:@"HistoryXIBName" bundle:nil];
    cell = (HistoryCell *)*aNewCell.view;
}

含まれているビューのXIBファイル内のセルへのアウトレットを使用する別の方法があります。これは、iOS4が最新のときに使用していたものよりも少し複雑です。編集したバージョンが機能しない場合は、古いプロジェクトを掘り下げて、それをどのように行ったかを思い出します。

于 2013-03-21T20:33:10.470 に答える
0

xibでtableViewCellを使用する場合、インスタンス化はxibからロードされます。

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

    static NSString *CellIdentifier = @"historyCellType";
    HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"HistoryCell"
                                         owner:nil
                                       options:nil]lastObject];
    }

    cell.nameLabel.text = @"Donald Duck";

    return cell;

    }
于 2013-03-21T20:35:17.820 に答える
0
- (UITableViewCell *)tableView:(UITableView *)tableView 
       cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellidentifier=@"cell1"; 
    customcell *cell =
      (customcell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier];

    if (cell==nil)  {

    [tableView registerNib:[UINib nibWithNibName:@"Customcell" bundle:nil] 
               forCellReuseIdentifier:cellidentifier]; 

    cell =
     (customcell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier 
                 forIndexPath:[NSIndexPath indexPathForItem:indexPath.row 
                                                  inSection:indexPath.section]];   
    }   

    return cell; 
}
于 2014-03-27T13:34:35.873 に答える