0

在 tableview 上滚动图像时出现问题。

我收到信号“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;

}   

提前致谢。

4

1 回答 1

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;
}   
于 2010-04-09T12:42:20.553 回答