2012-10-16 51 views
0

真的需要幫助。我到處尋找,但仍無法找到答案。所以,當我用自定義單元格滾動我的UITableView時,每次看到它時都會重新創建單元格。因此,它顯着降低了我的表現。自定義UITableViewCell重新創建自己

我的方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSString *CellIdentifier = @"CellWithViewForProduct"; 
ProductCell *cell = (ProductCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
ProductDataStruct *dataStruct = [self.arrayData objectAtIndex:indexPath.row]; 
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"productCell" owner:nil options:nil]; 
    for(id currentObject in topLevelObjects) 
    { 
     if([currentObject isKindOfClass:[ProductCell class]]) 
     { 
      cell = (ProductCell *)currentObject; 
      break; 
     } 
    } 
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 
formatter.roundingIncrement = [NSNumber numberWithDouble:0.01]; 
formatter.numberStyle = NSNumberFormatterDecimalStyle; 

NSString *price = [formatter stringFromNumber:[NSNumber numberWithFloat:(dataStruct.price.floatValue * [[[NSUserDefaults standardUserDefaults] objectForKey:@"CurrencyCoefficient"] floatValue])]]; 
NSString *priceString = [NSString stringWithFormat:@"%@ %@", price, [[NSUserDefaults standardUserDefaults] objectForKey:@"Currency"]]; 

cell.productPrice.text = priceString; 
cell.productDescription.text = [NSString stringWithFormat:@"%@", dataStruct.descriptionText]; 
cell.productTitle.text = [NSString stringWithFormat:@"%@", dataStruct.title]; 

if (dataStruct.hit.integerValue == 1) 
{ 
    [cell.productImage.layer addSublayer:[hitView layer]]; 
} 
else 
    if (dataStruct.hit.integerValue == 2) 
     [cell.productImage.layer addSublayer:[newsItemView layer]]; 

if (!dataStruct.image) 
{ 
    if (self.tableView.dragging == NO && self.tableView.decelerating == NO && dataStruct.link) 
    { 
     [self startIconDownload:dataStruct forIndexPath:indexPath]; 
    } 
    // if a download is deferred or in progress, return a placeholder image 
    //cell.productImage.image = [UIImage imageNamed:@"Placeholder.png"]; 

    // if a download is deferred or in progress, return a loading screen 
    cell.productImage.alpha = 0; 
    [cell.productImageLoadingIndocator startAnimating]; 

} 
else 
{ 
    if (self.didLoad) 
    { 
     cell.productImage.alpha = 0; 
    } 
    [cell.productImageLoadingIndocator stopAnimating]; 
    cell.productImage.image = dataStruct.image; 
    [self imageAnimation: cell.productImage]; 
} 
NSLog(@"WAZAAA"); 
return cell; 
} 
+1

你說的「這是重新創建」的意思 - 這是什麼重現呢?怎麼樣? – 2012-10-16 16:41:35

+0

單元格不是「每次創建」的。他們創建一次。 – 2012-10-16 16:42:37

+0

在表中我有40個元素。當我向下滾動時 - 消息出現80次 – NewObjective

回答

1

這整個的代碼塊:

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

看來我錯了。在它上面,

ProductCell *cell = (ProductCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

應該足以讓你一個細胞使用。

0

首先創建的.h

一個@property (nonatomic, assign) IBOutlet ProductCell *prodCell;然後在.M你只需要這個

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

NSString *CellIdentifier = @"CellWithViewForProduct"; 

    ProductCell *cell = (ProductCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     [[NSBundle mainBundle] loadNibNamed:@"productCell" owner:self options:nil]; 

     cell = prodCell; 

     self.prodCell = nil; 
     NSLog(@"WAZAAA"); //Now it will only be called when a new cell is created. 
    } 

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 
formatter.roundingIncrement = [NSNumber numberWithDouble:0.01]; 
formatter.numberStyle = NSNumberFormatterDecimalStyle; 

NSString *price = [formatter stringFromNumber:[NSNumber numberWithFloat:(dataStruct.price.floatValue * [[[NSUserDefaults standardUserDefaults] objectForKey:@"CurrencyCoefficient"] floatValue])]]; 
NSString *priceString = [NSString stringWithFormat:@"%@ %@", price, [[NSUserDefaults standardUserDefaults] objectForKey:@"Currency"]]; 

cell.productPrice.text = priceString; 
cell.productDescription.text = [NSString stringWithFormat:@"%@", dataStruct.descriptionText]; 
cell.productTitle.text = [NSString stringWithFormat:@"%@", dataStruct.title]; 

if (dataStruct.hit.integerValue == 1) 
{ 
    [cell.productImage.layer addSublayer:[hitView layer]]; 
} 
else 
    if (dataStruct.hit.integerValue == 2) 
     [cell.productImage.layer addSublayer:[newsItemView layer]]; 

if (!dataStruct.image) 
{ 
    if (self.tableView.dragging == NO && self.tableView.decelerating == NO && dataStruct.link) 
    { 
     [self startIconDownload:dataStruct forIndexPath:indexPath]; 
    } 
    // if a download is deferred or in progress, return a placeholder image 
    //cell.productImage.image = [UIImage imageNamed:@"Placeholder.png"]; 

    // if a download is deferred or in progress, return a loading screen 
    cell.productImage.alpha = 0; 
    [cell.productImageLoadingIndocator startAnimating]; 

} 
else 
{ 
    if (self.didLoad) 
    { 
     cell.productImage.alpha = 0; 
    } 
    [cell.productImageLoadingIndocator stopAnimating]; 
    cell.productImage.image = dataStruct.image; 
    [self imageAnimation: cell.productImage]; 
} 
NSLog(@"WAZAAA"); //Here doesn't make any sense, the cell is returned for being displayed, this doesn't mean it's being created. 
return cell; 
} 
+0

我可能錯過了它,但是唯一一次我看到你使用這個屬性的時候,它被設置爲零 - 爲什麼這個屬性是必要的? – Isaac

+0

該屬性有一個IBOutlet,所以你可以將它鏈接到一個自定義單元筆尖。您只在單元格爲零時從筆尖創建一個新單元 – jcesarmobile

+0

即使作爲IBOutlet,我也沒有看到您訪問該屬性 - 唯一一次'self.prodCell'出現在您的代碼中的行是'self.prodCell =無;'。 – Isaac