本文介绍了UIImageView 未显示在自定义 UITableViewCell 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下:

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

}

由于某种原因,图像没有显示出来.我在这里做错了什么?图片的网址有效,我查过了.

For some reason the image is not showing up. What am I doing wrong here? The URL of the image is valid, I checked.

此外,我不想在 TableViewCell 上有附件视图,我确实在 IB 中将其指定为 no,但它仍然显示...我已经将附件属性设置为 none

Also I don't want to have the accessory view on the TableViewCell, I did specify that as no in IB, but it still shows up... i set the accessory attribute to none already

推荐答案

您的代码存在一些问题.

you have some issues in your code.

NSData *imgData = [[[NSData dataWithContentsOfURL:
                     [NSURL URLWithString:
                      picURL]] autorelease] retain];

前者不是真正的问题(也许是巧合?!),但它丑陋且令人困惑.摆脱自动释放和保留.您从 dataWithContentsOfURL: 获得的对象已经自动释放.

the former is not a real issue (maybe by coincidence?!), but it's ugly and confusing. get rid of the autorelease and the retain. The object you get from dataWithContentsOfURL: is already autoreleased.

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

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

And your real issue might be this. From what I know you should call init exactly one time; after you've allocated an object.

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

So if cell.pic is an UIImageView I would try to change the code to something like this:

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

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


Once you see your images you should change your code so that it saves the images in the data you use to populate the cell. You are downloading the image every time a cell appears. That's something you shouldn't do. But this is beyond this question.

我也不想拥有TableViewCell 上的附件视图,我确实在 IB 中指定为 no,但它仍然出现... 我设置了配件已经归于无

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

You do? But I can see this line of code:

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

这篇关于UIImageView 未显示在自定义 UITableViewCell 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:42