【问题标题】:Tableview loading more than 50 images Received memory warningTableview加载超过50张图片收到内存警告
【发布时间】:2013-12-28 13:22:44
【问题描述】:

这是我的手机数据

for (int s=0; s<[arrayForPhotosData1 count]; s++) {
    PhotoCell *cell=[[PhotoCell alloc] init];
    cell.imgPhoto.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    cell.imgPhoto.layer.shadowOffset = CGSizeMake(0, 2);
    cell.imgPhoto.layer.shadowOpacity = 1;
    cell.imgPhoto.layer.shadowRadius = 1.0;
    cell.imgPhoto.clipsToBounds = NO;
    if([[arrayForPhotosData1 objectAtIndex:s] valueForKey:@"resolution_280_280"]!=NULL)
    {
        [cell.imgPhoto loadImage:[[arrayForPhotosData1 objectAtIndex:s] valueForKey:@"resolution_280_280"]];

    }
    [cell.ImgProfile loadImage:[[arrayForPhotosData1 objectAtIndex:s] valueForKey:@"profile_picture"]];
    [cell.lblUserName setText:[[arrayForPhotosData1 objectAtIndex:s] valueForKey:@"caption"]];
    [cell.lblForTime setText:[[arrayForPhotosData1 objectAtIndex:s] valueForKey:@"created_time"]];
    [cell.lblFullName setText:[[arrayForPhotosData1 objectAtIndex:s] valueForKey:@"Name" ]];
    [cell.btnForLike addTarget:self
                        action:@selector(addlikes:)
              forControlEvents:UIControlEventTouchUpInside];

    [cell.btnForLike  setImage:[UIImage imageNamed:@"like.png"] forState:UIControlStateNormal];
    [cell.btnForFavourite addTarget:self
                             action:@selector(addFavourite:)
                   forControlEvents:UIControlEventTouchUpInside];
    cell.btnForFavourite.tag=[[[arrayForPhotosData1  objectAtIndex:s]valueForKey:@"mediaid"] intValue];
    cell.btnForComment.tag=[[[arrayForPhotosData1  objectAtIndex:s]valueForKey:@"mediaid"] intValue];
    [cell.btnForComment addTarget:self action:@selector(addcomen:)forControlEvents:UIControlEventTouchUpInside];
    cell.imgPhoto.tag=[[[arrayForPhotosData1  objectAtIndex:s]valueForKey:@"mediaid"] intValue];
    [cell.imgPhoto addTarget:self action:@selector(addcoments:)forControlEvents:UIControlEventTouchUpInside];
    cell.btnForLike.tag=s;

    [cell.btnForLikeText setTitle:[NSString stringWithFormat:@"%@ Me gusta",[[arrayForPhotosData1 objectAtIndex:s] valueForKey:@"Totallikes"]] forState:UIControlStateNormal];

    [cell.btnForCommentText setTitle:[NSString stringWithFormat:@"%@ Commentarios",[[arrayForPhotosData1 objectAtIndex:s] valueForKey:@"TotalComment"]] forState:UIControlStateNormal];

    [cell.btnForLikeText addTarget:self
                            action:@selector(addlikes:)
                  forControlEvents:UIControlEventTouchUpInside];
    cell.btnForLikeText.tag=s;
    cell.btnForCommentText.tag=s;
    cell.btnForFavouritetext.tag=s;
    [cell.btnForFavouritetext addTarget:self
                                 action:@selector(addFavourite:)
                       forControlEvents:UIControlEventTouchUpInside];
    [cell.btnForCommentText addTarget:self action:@selector(addcomen:)forControlEvents:UIControlEventTouchUpInside];



    if([[[arrayForPhotosData1 objectAtIndex:s] valueForKey:@"FavoriteIsActive"] isEqualToString:@"true"])
    {

        [cell.btnForFavouritetext setTitle:[NSString stringWithFormat:@"%@ ",@"Favoritos"] forState:UIControlStateNormal];
    }
    [cell.btnForFavouritetext setTitle:[NSString stringWithFormat:@"%@ ",@"Favoritos"] forState:UIControlStateNormal];
    [arrayForCell addObject:cell];
}

[tableViewForPhotos reloadData];

这是我在单元格中显示的数据

static NSString *simpleTableIdentifier1 = @"TableItem1";

PhotoCell *cell1 = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier1];

if (cell1 == nil) {
    cell1 = [[PhotoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier1];
}

if ([arrayForCell count]>0) {
    cell1= [arrayForCell objectAtIndex:indexPath.row-1];

    cell1.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell1;
}

加载 50 张图片后,应用收到 Received memory 警告。

【问题讨论】:

    标签: ios uitableview didreceivememorywarning


    【解决方案1】:

    没有必要检查 cell1 是否为 nil。如果你注册了[PhotoCell class],那么它永远不会是nil。

    我相信您收到内存警告是因为您没有重复使用单元格。

    这里:

    cell1= [arrayForCell objectAtIndex:indexPath.row-1];
    

    你想达到什么目的?

    【讨论】:

    • Matteo Pacini 在 indexpath 0 中我创建了另一个单元格,这就是我使用 [indexpath.row-1] 的原因
    • 问题是:你将一个可重用单元(存储在 cell1 中)出列,然后当控制到达这一行时你将其丢弃 cell1= [arrayForCell objectAtIndex:indexPath.row-1]
    • 那么解决办法是什么
    • 你能告诉我解决方案我正在苦苦挣扎 5 天
    • 你创建单元格的方式是完全错误的。您应该在 cellForRowAtIndexPath 中将它们出列。在 MVC 范例中,您应该使用模型数组(例如,填充单元格的数据)而不是单元格数组。
    猜你喜欢
    • 2014-01-09
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多