0

テーブル ビュー コントローラーとカスタム セルを作成しました。カスタム セルのラベル用のアウトレットを持つ新しい tableviewCell ファイルを作成しました。

tableviewController に tableviewcell クラスをインポートし、次のコードで配列からセルに値を代入しようとしました:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

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

cell.name.text= [data objectAtIndex:indexPath.row];

cell.name -----------(tableviewcell クラスの uilabel である名前が表示されない、ヘルプにも存在しない!、理由がわかりません)

以下のコードは、ラベルにタグを付けることで機能します

    // Configure the cell...

//    UILabel *label = (UILabel *)[cell viewWithTag:111];
//              label.text= [data objectAtIndex:indexPath.row];

私の質問は、タグ付けせずにコンセント部分を機能させる方法と、なぜ uilabel が tableviewCONtroller に表示されないのですか?

助けてください。助けていただければ幸いです。

前もって感謝します。

4

2 に答える 2

3

カスタムセルのクラス名が「MyCustomTableCell」で、UITableViewCellからサブクラス化されている場合、コードは次のようになります。

static NSString *CellIdentifier = @"Cell";
MyCustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier   forIndexPath:indexPath];

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

その後、そのプロパティにアクセスできます

cell.name = @"cell name";
于 2013-03-14T03:03:07.987 に答える
3

Nib をロードすることを忘れないでください:

static NSString *CellIdentifier = @"CellIdentifier";
 MyCustomTableCell *cell = (MyCustomTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[MyCustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCustomTableCell" owner:self options:nil] lastObject];
       // UINib *theNib = [UINib nibWithNibName:@"MyCustomTableCell" bundle:nil];
       // cell = [[theNib instantiateWithOwner:self options:nil] lastObject];
    }
于 2013-03-14T03:06:57.920 に答える