滚动带有图像的tableview时发出“0”错误信号

时间:2010-04-09 12:30:55

标签: iphone uitableview memory-leaks uiimage dealloc

我在桌面视图上滚动图片时遇到问题。

我收到信号“0”错误。

我认为这是由于一些内存问题,但我无法找出确切的错误。 代码如下,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [travelSummeryPhotosTable dequeueReusableCellWithIdentifier:CellIdentifier];

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

    }

    //Photo ImageView
    UIImageView *photoTag = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, 5.0, 85.0, 85.0)];

    NSString *rowPath =[[imagePathsDictionary valueForKey:[summaryTableViewDataArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

    photoTag.image = [UIImage imageWithContentsOfFile:rowPath];
    [cell.contentView addSubview:photoTag];

    [photoTag release];

    // Image Caption
    UILabel *labelImageCaption = [[UILabel alloc] initWithFrame:CGRectMake(110.0, 15.0, 190.0, 50.0)];
    labelImageCaption.textAlignment = UITextAlignmentLeft;
    NSString *imageCaptionText =[   [imageCaptionsDictionary valueForKey:[summaryTableViewDataArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
    labelImageCaption.text = imageCaptionText;
    [cell.contentView addSubview:labelImageCaption];
    [labelImageCaption release];

    return cell;

}   

UITableViewCell *cell = [travelSummeryPhotosTable dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease]; } //Photo ImageView UIImageView *photoTag = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, 5.0, 85.0, 85.0)]; NSString *rowPath =[[imagePathsDictionary valueForKey:[summaryTableViewDataArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; photoTag.image = [UIImage imageWithContentsOfFile:rowPath]; [cell.contentView addSubview:photoTag]; [photoTag release]; // Image Caption UILabel *labelImageCaption = [[UILabel alloc] initWithFrame:CGRectMake(110.0, 15.0, 190.0, 50.0)]; labelImageCaption.textAlignment = UITextAlignmentLeft; NSString *imageCaptionText =[ [imageCaptionsDictionary valueForKey:[summaryTableViewDataArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; labelImageCaption.text = imageCaptionText; [cell.contentView addSubview:labelImageCaption]; [labelImageCaption release]; return cell; }

提前致谢。

1 个答案:

答案 0 :(得分:2)

信号“0”错误通常意味着应用程序因内存不足而崩溃。

您的问题似乎如下 - 您每次调用cellForRowAtIndexPath方法时都会创建单元子视图并将它们添加到您的单元格中 - 因此所有分配的UI元素都会挂起内存,应用程序最终会从中获取。

要解决您的问题,您必须只创建一次单元格的子视图 - 创建它时以及稍后设置它们的内容:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [travelSummeryPhotosTable dequeueReusableCellWithIdentifier:CellIdentifier];

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

        UIImageView *photoTag = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, 5.0, 85.0, 85.0)];
        photoTag.tag = 10;
        [cell.contentView addSubview:photoTag];
        [photoTag release];

        UILabel *labelImageCaption = [[UILabel alloc] initWithFrame:CGRectMake(110.0, 15.0, 190.0, 50.0)];
        labelImageCaption.tag = 11;
        labelImageCaption.textAlignment = UITextAlignmentLeft;
        [cell.contentView addSubview:labelImageCaption];
        [labelImageCaption release];
    }

    //Photo ImageView
    NSString *rowPath =[[imagePathsDictionary valueForKey:[summaryTableViewDataArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
    UIImageView* photoTag = (UIImageView*)[cell.contentView viewWithTag:10];
    photoTag.image = [UIImage imageWithContentsOfFile:rowPath];

    // Image Caption
    UILabel *labelImageCaption = (UILabel*)[cell.contentView viewWithTag:11];
    NSString *imageCaptionText =[   [imageCaptionsDictionary valueForKey:[summaryTableViewDataArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
    labelImageCaption.text = imageCaptionText;

    return cell;
}