在uitbaleviewcell里面的tableview上显示图像

时间:2011-10-12 11:16:57

标签: iphone uitableview uiimage

以下是我用来在表格视图中显示图像的代码及其名称。它工作正常但是当我们在文件夹中有很多图像时,应用程序崩溃了。任何帮助是极大的赞赏。

NSString *CellIdentifier = @"DocumentList";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


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

}

   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *bundleRoot = [paths objectAtIndex:0];

    NSString *dataPath = [bundleRoot stringByAppendingPathComponent:[NSString stringWithFormat:@"%d", 1]];
    NSString *imagePath = [NSString stringWithFormat:@"%@/%@", dataPath, [itsDocumentNamesArray objectAtIndex:indexPath.row]];

    [cell.imageView setImage:[[[UIImage alloc] initWithContentsOfFile:imagePath] autorelease]];
    cell.textLabel.text = [itsDocumentNamesArray objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:15.0];
    cell.textLabel.textColor = [UIColor grayColor];

1 个答案:

答案 0 :(得分:0)

    UIImageView *temp=[[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 60, 60)]; //40 
    temp.clipsToBounds=YES;
    temp.tag=10;
    temp.userInteractionEnabled=YES;
    temp.layer.cornerRadius=8.0;
    [cell.contentView addSubview:temp];
    [temp release];

将上面的部分放在cell == nil块和以下代码之外......

    UIImageView *temp=(UIImageView*)[cell.contentView viewWithTag:10];
    NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    NSString *docDirectory = [sysPaths objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", docDirectory,[tableData objectAtIndex:indexPath.row]];
    UIImage *cellImage=[UIImage imageWithContentsOfFile:filePath];
    temp.image=cellImage;