Iphone UIImageView未显示在自定义UITableViewCell中

Iphone UIImageView未显示在自定义UITableViewCell中,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,代码如下: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"ConvoreCell"; ConvoreCell * cell = (ConvoreCell *) [tableView dequeueReusableCellWithIdentifier:CellI

代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"ConvoreCell";

ConvoreCell * cell = (ConvoreCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ConvoreCell" owner:nil options:nil];
    for (id currentObject in topLevelObjects){
        if ([currentObject isKindOfClass:[UITableViewCell class]]){
            cell = (ConvoreCell *) currentObject;
            break;
        }
    }
}

NSMutableDictionary *cellValue = [results objectAtIndex:indexPath.row];

NSString *picURL = [[cellValue objectForKey:@"creator"] objectForKey:@"img"];
if ([picURL isEqualToString:@"https://secure.gravatar.com/avatar/1f043010eb1652b3fab3678167dc0487/?default=https%3A%2F%2Fconvore.com%2Fmedia%2Fimages%2Feric.png&s=80"])
    picURL = @"https://convore.com/media/images/eric.png";

if ((picURL != (NSString *) [NSNull null]) && (picURL.length !=0)) {
    NSLog(@"%@", picURL);
    NSData *imgData = [[[NSData dataWithContentsOfURL:
                         [NSURL URLWithString:
                          picURL]] autorelease] retain];
    [cell.pic initWithImage:[[UIImage alloc] initWithData:imgData]];

} else {
    cell.pic = nil;
}


//NSLog(@"Name is : %@", [cellValue objectForKey:@"name"]);
cell.title.text = [cellValue objectForKey:@"name"];
cell.info.text = (@"Created by: %@", [[cellValue objectForKey:@"creator"] objectForKey:@"name"]);
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;



// Configure the cell...

return cell;
}

由于某种原因,图像没有显示出来。我做错了什么?图像的URL是有效的,我检查过了


另外,我不想在TableViewCell上显示附件视图,我确实在IB中指定为“否”,但它仍然显示。。。我已经将附件属性设置为“无”

您只需

[cell.pic initWithImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSUrl NSURL picURL]]]];

您没有显示cellForRowAtIndexPath方法,这是分析发生了什么的关键。典型的范例是UITableViewCell对象被UITableView重复使用。当UITableView调用cellForRowAtIndexPath时,应该将UIImage设置到单元格中

您的代码中有一些问题

NSData *imgData = [[[NSData dataWithContentsOfURL:
                     [NSURL URLWithString:
                      picURL]] autorelease] retain];
前者不是一个真正的问题(也许是巧合?!),但它丑陋而令人困惑。摆脱自动释放和保留。从dataWithContentsOfURL:获取的对象已自动删除


你真正的问题可能是这个。据我所知,你应该只调用init一次;在分配对象之后

因此,如果cell.pic是UIImageView,我会尝试将代码更改为以下内容:

cell.pic.image = [[[UIImage alloc] initWithData:imgData] autorelease];

看到图像后,应更改代码,以便将图像保存在用于填充单元格的数据中。每次出现单元格时,您都在下载图像。那是你不应该做的事。但这是超越这个问题的

编辑:

而且我也不想有 TableViewCell上的附件视图,I 确实在IB中指定为“否”,但 仍然出现我设置了附件 已将属性设置为“无”

是吗?但我可以看到这行代码:

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

导致您立即崩溃的问题是,ConvalRecell的nib正在尝试设置您声明的IBOutlet,但却找不到实际设置它的方法。我会检查你是否合成了你的pic属性。 这里还有其他问题。线路

    [cell.pic initWithImage:[[UIImage alloc] initWithData:imgData]];
这是错误的;您不会对已经存在的对象调用init,但仅在分配对象时才调用。做点像

cell.pic = [[[WhateverClassPicIS alloc] initWithImage:[[UIImage alloc] initWithData:imgData]];

此外,您正在主线程上使用NSData DATA with contentsOfURL。当您连接到网络时,这将在不确定的时间内锁定您的用户界面。改用异步调用

你能给我指一个可以保存图像的资源吗?
cell.pic = [[[WhateverClassPicIS alloc] initWithImage:[[UIImage alloc] initWithData:imgData]];