如何在应用程序中缓存单元格图像以不接收内存警告

时间:2014-07-07 23:13:08

标签: ios objective-c xcode macos

我想知道如何从后台单元缓存图像以不接收图像。这些图像用于表视图中的数组。 viewdidload中的数组如下所示:

Cars *car1 = [Cars new];
car1.name = @"BMW 114i";
car1.price = @"28,000 $";
car1.horsepower = @"102hp (75kW)";
etc... 

在这个viewdidload的底部是带有对象的数组,它有大约400多项,所以设备有点问题所以我有很多内存警告

以下是代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomTableCell";
    CarsCell *cell = (CarsCell *) [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell ==nil){
        cell = [[CarsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    Cars *cars = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cars = [searchResults objectAtIndex:indexPath.row];
    }else{
        cars = [carss objectAtIndex:indexPath.row];
    }

    UIImage *backgroundImage = [UIImage imageNamed:cars.image];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:backgroundImage];

    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    imageView.alpha = 0.8f;
    cell.backgroundView = imageView;

    cell.nameLabel.text = cars.name;
    cell.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.2f];
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.nameLabel.backgroundColor = [UIColor clearColor];
    cell.nameLabel.textColor = [UIColor colorWithWhite:1.0f alpha:0.8f];

    return cell;
}

0 个答案:

没有答案