1

私はカスタムUITableViewCellサブクラスを持っており、これが iOS 5 のカスタム セルをロードする正しい方法であると思われることを読みました。

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

   if (cell == nil) {
      cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"customCell"];

      // Configure cell
      cell.nameLabel.text = self.customClass.name;
   }

   return cell;
}

しかし、アプリを実行してもラベル テキストは表示されません。ただし、次の方法も試しました。

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

   if (cell == nil) {
      NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];

   for (UIView *view in views) {
        if([view isKindOfClass:[UITableViewCell class]])
        {
            cell = (CustomCell *)view;
        }
    }

    // Configure cell
    ((CustomCell *)cell).nameLabel.text = self.customClass.name;
}

return cell;

}

このようにラベルが表示されますが、anyreuseIdentifierはメソッドで設定されloadNibName:ます。カスタムセルをロードする最良の方法は何ですか? iOS 5 以降をサポートする必要があります。initWithStyle:テーブルビューのメソッドではなく、メソッドでセルのラベルとスタイルを構成する必要があるため、最初のアプローチが機能しない可能性がありますか?

ありがとう!

4

3 に答える 3

3

コードのエラーはcell.nameLabel.text、ケースでのみ設定され、if (cell == nil) { ... }一般的には設定されないことです。

カスタム セルをロードする最も簡単な方法は次のとおりです。

  • registerNib:forCellReuseIdentifier:セルがnibファイルで定義されている場合に使用するか、または
  • registerClass:forCellReuseIdentifier:セルがプログラムで作成された場合に使用する、または
  • ストーリーボードを使用し、「CustomCell」をテーブル ビューの「プロトタイプ セル」として定義します。

例 ( viewDidLoad):

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

次にdequeueReusableCellWithIdentifier 、常にセルを返すため、次のようにcellForRowAtIndexPath簡略化されます

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CustomCell *cell = [tv dequeueReusableCellWithIdentifier:@"customCell"];
   cell.nameLabel.text = self.customClass.name;
   return cell;
}
于 2013-08-14T08:57:18.673 に答える
0

このコードを使用します (NewCustomCell はカスタム UITableViewCell クラス名です)...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    NewCustomCell *cell = (NewCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"NewCustomCell" owner:self options:nil];
        cell=[nib objectAtIndex:0];
    }

    return cell;
}

幸せなコーディング... :)

于 2013-08-14T09:17:03.730 に答える
0

このコードを使用して試してみてください。

static NSString *cellIdentifier=@"cell";
    YourCustomCell *cell = (YourCustomCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {
        NSString *customeCellName = [NSString stringWithFormat:@"%@",[YourCustomCell class]];

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:customeCellName owner:self options:nil];

        for (id currentObject in topLevelObjects)
        {
            if ([currentObject isKindOfClass:[UITableViewCell class]])
            {
                cell =  (YourCustomCell *) currentObject;
                break;
            }
        }
    }
    cell.nameLabel.text = self.customClass.name; // Make sure self.customclass.name have value.
    return cell;
于 2013-08-14T08:55:05.697 に答える