0

tableView:cellForRowAtIndexPath: 内からカスタム テーブル セルを処理する方法に非常に興味があります。

viewDidLoad でカスタム テーブル セルをインスタンス化します。私の質問は、再利用可能なセルがない場合に cellForRowAtIndexPath の状況をどのように処理するかです。たとえば、検索結果を返すとき。必要な場合、新しいカスタム セルを作成するにはどうすればよいですか? 以下に関連するメソッドを含めました。

ありがとう

 -(void)viewDidLoad
 {
   [super viewDidLoad];

   //Load custom table cell
     UINib *customCellNib = [UINib nibWithNibName:@"CustomItem" bundle:nil];

   //Register this nib, which contains the cell
    [[self tableView] registerNib:customCellNib forCellReuseIdentifier:@"CustomItemCell"];


   //.... More stuff here

  }


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

     CustomItemCell *cell =
    [tableView dequeueReusableCellWithIdentifier:@"CustomItemCell"];

     // If there is no reusable cell of this type, create a new one
     if (!cell) {


       //Is this right?
       cell = [[CustomItemCell alloc] init];

     }




          //Set up cell with data here...



    return cell;
 }
4

1 に答える 1

0

メインバンドルにアクセスして、カスタムテーブルセルnibファイルを探して、これを解決することになりました。カスタム テーブル セル クラスが見つかったら、iVar「セル」をそのオブジェクトに割り当てます。

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

   static NSString *cellID = @"CustomItemCell";
   CustomItemCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

   // If there is no reusable cell of this type, create a new one

   if(cell == nil){


    NSArray *nibObjects = [[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:nil options:nil];
    for (id currentObject in nibObjects) {
        if([currentObject isKindOfClass:[CustomItemCell class]])
        {
            cell = (CustomItemCell *)currentObject;

        }


    }

  } 
   //Assign data to cell here   
}
于 2012-06-28T19:51:17.200 に答える