在uitbaleviewcell内部的tableview上显示图像 [英] Display image on tableview inside uitbaleviewcell

查看:47
本文介绍了在uitbaleviewcell内部的tableview上显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The following is the code I am using to display image in the table view and its name. It works fine however when we have lot of images inside the folder the app crashes. Any help is greatly appreciated.

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];

推荐答案

    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块中,并将下面的代码放在外部...

Have the above part within the cell==nil block and the following piece of code outside...

    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;

这篇关于在uitbaleviewcell内部的tableview上显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆