Сообщение UITableView «получено предупреждение о памяти»

Я тестирую свое приложение с iPod touch5 и получил предупреждение о памяти,

Я использую свой собственный UITableViewCell, как я могу улучшить свой код? Большое тебе спасибо.

Клетка:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code

    self.frame = CGRectMake(0.0, 0.0, 320.0, 255.0);


    self.shot_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 225.0)];
    [self addSubview:self.shot_imageView];

    UIImageView *viewsImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15.0, 231.0, 13.0, 13.0)];
    viewsImageView.image = [UIImage imageNamed:@"eye"];
    [self addSubview:viewsImageView];

    self.viewsLabel = [[UILabel alloc] initWithFrame:CGRectMake(33.0, 230.0, 65.0, 15.0)];
    [self addSubview:self.viewsLabel];

    UIImageView *likesImageView = [[UIImageView alloc] initWithFrame:CGRectMake(150.0, 233.0, 10.0, 10.0)];
    [self addSubview:likesImageView];

    self.likesLabel = [[UILabel alloc] initWithFrame:CGRectMake(165.0, 230.0, 45.0, 15.0)];
    [self addSubview:self.likesLabel];


    UIImageView *commentsImageView = [[UIImageView alloc] initWithFrame:CGRectMake(270.0, 233.0, 10.0, 10.0)];
    [self addSubview:commentsImageView];

    self.commentsLabel = [[UILabel alloc] initWithFrame:CGRectMake(285.0, 230.0, 35.0, 15.0)];
    [self addSubview:self.commentsLabel];
}

return self;
}

Таблица:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section = [indexPath section];

static NSString *CellIdentifier = @"Cell";
Shots *shot =  [self.dataSource objectAtIndex:section];
//NSString *url = shot.image_url;
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ( cell == nil )
{
    cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

//[cell.shot_imageView setImageWithURL:[NSURL URLWithString:shot.image_url] placeholderImage:[UIImage imageNamed:@"placeholder"]];
UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicatorView.center = cell.shot_imageView.center;
[activityIndicatorView startAnimating];
[cell.shot_imageView addSubview:activityIndicatorView];
__weak typeof(cell) weakCell = cell;
[cell.shot_imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:shot.image_url]]
                            placeholderImage:[UIImage imageNamed:@"placeholder"]
                                     success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                         [activityIndicatorView stopAnimating];
                                         [activityIndicatorView removeFromSuperview];
                                         weakCell.shot_imageView.image = image;
                                     }
                                     failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                         [activityIndicatorView stopAnimating];
                                         [activityIndicatorView removeFromSuperview];
                                         [weakCell.shot_imageView setImage:[UIImage imageNamed:@"placeholder"]];
                                     }];

cell.likesLabel.text = [NSString stringWithFormat:@"%@",shot.likes_count];
cell.viewsLabel.text = [NSString stringWithFormat:@"%@",shot.views_count];
cell.commentsLabel.text = [NSString stringWithFormat:@"%@",shot.comments_count];
cell.selectionStyle = UITableViewCellSelectionStyleNone;

return cell;

}

person jxdwinter    schedule 18.10.2013    source источник


Ответы (1)


Предупреждение о памяти не является проблемой само по себе. Насколько я понимаю в setImageWithURLRequest:, он использует NSURLCache по умолчанию для кэширования изображений, и кеш будет освобождаться системой всякий раз, когда будет получено предупреждение о памяти. Таким образом, ваша память никогда не должна превышать установленный лимит.

Кстати, улучшением было бы размещение UIActivityIndicatorView внутри ячейки. В вашем текущем коде вы перераспределяете его каждый раз, когда ячейка используется повторно...

person slecorne    schedule 18.10.2013
comment
Спасибо. Я поместил ActivityIndicatorView в свою пользовательскую ячейку. - person jxdwinter; 19.10.2013