2012-05-02 4 views
3

URL接続を使用して非同期にコンテンツをロードするカスタムUITableViewCellサブクラスを作成したいと考えています。私は、このすべてと、セルのレイアウトを定義するNibファイルを処理するUITableViewCellサブクラスを持っていますが、私は2つのリンクに問題があります。ここで私はtableView:cellForRowAtIndexPathで使用しているコードです。独自のNibでUITableViewCellをサブクラス化する

static NSString *FavCellIdentifier = @"FavCellIdentifier"; 

FavouriteCell *cell = [tableView dequeueReusableCellWithIdentifier:FavCellIdentifier]; 

if (cell == nil) 
{ 
    cell = [[[FavouriteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FavCellIdentifier] autorelease]; 
} 

cell.requestURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@?%@=%i", URL_GET_POST_STATUS, 
              URL_PARAM_SERIAL, 
              [[self.favourites objectAtIndex:indexPath.row] intValue]]]; 

return cell; 

これはsetRequestURL方法でロードを処理UITableViewCellのサブクラスへのリクエストのURLを提供します。

FavouriteCellクラスでは、initWithStyle:reuseIdentifier:メソッドをそのまま使用しています.Nibでは、FavCellIdentifierを識別子として、FavouriteCellをクラスとして設定しました。では、FavouriteCellクラスでNibをロードするにはどうすればいいですか?

答えて

7

nib/xibファイルを使用するには、別の方法でFavouriteCellをインスタンス化する必要があります。

これを試してみてください:

  1. があなたの代わりにあなたのXIBのデフォルトUITableViewCellFavouriteCellのサブクラスであるためにあなたのUITableViewCellタイプを変更したことを確認してください。これを行う方法は次のとおりです。
    • Interface Builderのオブジェクトペインでセルをクリックします。
    • 次に、[Identity Inspector]タブに移動して、[Custom Class]の下の[Class]選択がFavouriteCellになっていることを確認します。
  2. 変更するには(ステップ#1とほとんど同じプロセス)カスタムUITableViewCellを表示したいUIViewControllerなるようにFile's Ownerプロパティ。
  3. FavouriteCellIBOutletプロパティをUIViewControllerに追加します。あなたの好きな名前を付けてください(私はそれをcellと呼ぶつもりです)。
  4. UITableViewCellのxibに戻って、UITableViewCellにFile's OwnerのcellプロパティのIBOutletを接続します。
  5. プログラムでセルを読み込むために、このコードを使用します。

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

    static NSString *CellId = @"FavCellId"; 
    FavouriteCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId]; 
    if (!cell) { 
     // Loads the xib into [self cell] 
     [[NSBundle mainBundle] loadNibNamed:@"FavouriteCellNibName" 
             owner:self 
            options:nil]; 
     // Assigns [self cell] to the local variable 
     cell = [self cell]; 
     // Clears [self cell] for future use/reuse 
     [self setCell:nil]; 
    } 
    // At this point, you're sure to have a FavouriteCell object 
    // Do your setup, such as... 
    [cell setRequestURL:[NSURL URLWithString: 
        [NSString stringWithFormat:@"%@?%@=%i", 
         URL_GET_POST_STATUS, 
         URL_PARAM_SERIAL, 
         [[self.favourites objectAtIndex:indexPath.row] intValue]] 
    ]; 
    return cell; 
} 
関連する問題