2013-05-15 57 views
0

嗨im是textfiled,另一個用於images.and另一個用於button.the的事情是創建是沒有得到重用定製的tableview細胞和細胞越來越零每一次。你們可以幫我嗎?下面是代碼。重複使用多個自定義的tableview細胞

-(ProductCell *)getProductCell 
{ 
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ProductCell" owner:nil options:nil]; 
    ProductCell *cell; 
    for (id currentObject in topLevelObjects) 
    { 
     if ([currentObject isKindOfClass:[ProductCell class]]) 
     { 
      cell= (ProductCell*)currentObject; 
      return cell; 
     } 
    } 
    return nil; 
} 
-(RatingCell *)getRatingCell 
{ 
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RatingCell" owner:nil options:nil]; 
    RatingCell *cell; 
    for (id currentObject in topLevelObjects) 
    { 
     if ([currentObject isKindOfClass:[RatingCell class]]) 
     { 
      cell= (RatingCell*)currentObject; 
      return cell; 
     } 
    } 
    return nil; 
} 

-(NotifyCell *)getNotifyCell 
{ 
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"NotifyCell" owner:nil options:nil]; 
    NotifyCell *cell; 
    for (id currentObject in topLevelObjects) 
    { 
     if ([currentObject isKindOfClass:[NotifyCell class]]) 
     { 
      cell= (NotifyCell *)currentObject; 
      return cell; 
     } 
    } 
    return nil; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier1 = @"Cell1"; 
    static NSString *CellIdentifier2 = @"Cell2"; 
    static NSString *CellIdentifier3 = @"Cell3"; 

    self.productCell=(ProductCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 
    self.notifyCell = (NotifyCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; 
    self.ratingCell =(RatingCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier3]; 

    if (productCell == nil) productCell = [self getProductCell]; 
    if (notifyCell == nil) notifyCell = [self getNotifyCell]; 
    if (ratingCell == nil) ratingCell = [self getRatingCell]; 

    switch (indexPath.section) { 
     case 0: 
     self.productCell.lblName.text=[productTitleArray objectAtIndex:indexPath.row]; 
     return productCell; 
     break; 
     case 1: 
     self.productCell.lblName.text=[invoiceTitleArray objectAtIndex:indexPath.row]; 
     return productCell; 
     break; 
     case 2: 
     self.productCell.lblName.text=[warrantyTitleArray objectAtIndex:indexPath.row]; 
     return productCell; 
     break; 
     case 3: 
     [email protected]"Description"; 
     return productCell; 
     break; 
     case 4: 
     [email protected]"Rating"; 
     self.ratingCell.starRatingControl.delegate=self; 
     return self.ratingCell; 
     break; 
     case 5: 
     self.productCell.lblName.text=[serviceContactsTitleArray objectAtIndex:indexPath.row]; 
     return productCell; 
     break; 
     case 6: 
     self.notifyCell.lblName.text=[notifyTitleArray objectAtIndex:indexPath.row]; 
     return notifyCell; 
     break; 
     default: 
     break; 
    } 
    return productCell; 
    } 

回答

1

這裏有幾件事。

1)在託管您的表格視圖單元格的每個XIB文件中,確保您已在其上設置標識符以匹配「cellForRowAtIndexPath」方法中使用的標識符。例如。 「@"Cell3」爲RatingCells。

2)您需要註冊爲您從XIB加載對象的細胞再利用標識。

例如:

UINib * nib = [UINib nibWithNibName: @"RatingCell" bundle: nil]; 
if(nib) 
{ 
    [tableView registerNib: nib forCellReuseIdentifier:@"Cell3"]; 
} 

你可能需要做之前,你開始打電話「cellForRowAtIndexPath」(至少是我做的)。也許你可以在你的各種「get*Cell」方法派「tableView」參數。

+0

我應該使用上述線路的cellForRowAtIndexPath – user578386

+0

我不明白您的評論。 –

+0

我試着在get * cell方法中使用上面的代碼。我曾收到一個警告「不相容的指針類型發送的NSString類型UINib的放慢參數的」 – user578386