0

本当に助けが必要です。私はどこでも見ましたが、それでも答えが見つかりません。そのため、カスタムセルを含む UITableView をスクロールすると、表示されるたびにセルが再作成されます。その結果、パフォーマンスが大幅に低下します。

私の方法

-(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;
}
4

2 に答える 2

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];

セルを使用するのに十分なはずです。

于 2012-10-16T18:45:18.343 に答える
0

最初@property (nonatomic, assign) IBOutlet ProductCell *prodCell;.h

次に、.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;
}
于 2012-10-16T19:02:08.127 に答える