2012-05-02 35 views
3

我想創建一個異步使用URL連接加載內容的自定義的UITableViewCell子類。我有一個處理所有這些的UITableViewCell子類和一個定義單元格佈局的Nib文件,但是我在連接兩者時遇到了問題。下面是我使用的tableView:cellForRowAtIndexPath代碼:子類的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; 

這給出了一個請求URL到的UITableViewCell子類,它處理裝載在setRequestURL方法。

在FavouriteCell類中,我一直保留initWithStyle:reuseIdentifier:方法,並且在Nib中我將FavCellIdentifier設置爲標識符,將FavouriteCell設置爲類。現在我如何讓FavouriteCell類加載Nib?

回答

7

爲了使用筆尖/ XIB文件,你將不得不實例化FavouriteCell不同。

試試這個:

  1. 確保你已經改變了UITableViewCell的,要作FavouriteCell,而不是在你的廈門國際銀行默認UITableViewCell一個子類。通過這樣做:
    • 點擊Interface Builder中的對象窗格中的細胞。
    • 然後,轉到身份Inspector選項卡,並確保選擇類下定義類列出FavouriteCell
  2. 更改File's Owner屬性是UIViewController要顯示自定義UITableViewCell(幾乎相同的過程,步驟#1)。
  3. FavouriteCell類型的IBOutlet屬性添加到您的UIViewController。將它命名爲任何你喜歡的名稱(我將稱之爲cell)。
  4. 早在UITableViewCell的廈門國際銀行,在文件的所有者連接IBOutlet中的cell屬性自定義UITableViewCell
  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; 
} 
相關問題