7

所以我有以下代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = @"FriendsCell";

    FriendData * fd = [self.friendsDataSource_ objectAtIndex:indexPath.row];

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    [cell.imageView  setImageWithURL:[NSURL URLWithString:fd.imageUrl_]];
    [cell.imageView setFrame:CGRectMake(0, 0, 30, 30)];
    [cell.textLabel setText:fd.name_];

    return cell;
}

但是,我没有在单元格中看到 imageView。我究竟做错了什么?

4

4 回答 4

5

SDImageCache 也有同样的问题。我的解决方案是使用您想要的帧大小放置一个占位符图像。

 UIImage *placeholder = [UIImage imageNamed:@"some_image.png"];
 [cell.imageView setImage:placeholder];
 [cell.imageView setImageWithURL:[NSURL URLWithString:fd.imageUrl_]];
于 2012-06-11T14:06:11.917 回答
4

1) 使用 URL 设置图像不是 iOS 方法。这是定制的,这可能是问题所在。但在您发布之前,它无济于事。

2)我认为 cell.imageView 会忽略“setFrame”。我无法使用图像在我的任何表格单元格中使用它。它似乎总是默认为图像的宽度。

3) 通常您使用 cell.imageView.image = your_Image 设置图像。ImageView 是只读的,并且可能 ImageView 框架是私有的。

4)我认为您将需要创建自己的自定义单元格。

于 2012-05-22T23:03:45.860 回答
3

使用带有占位符的方法将修复它。

[cell.imageView setImageWithURL:[NSURL URLWithString:fd.imageUrl_]placeholderImage:[UIImage imageNamed:@"placeholder"]];
于 2013-06-30T07:02:28.880 回答
2

你需要return cell在方法结束时

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = @"FriendsCell";

    FriendData * fd = [self.friendsDataSource_ objectAtIndex:indexPath.row];

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    [cell.imageView  setImageWithURL:[NSURL URLWithString:fd.imageUrl_]];
    [cell.imageView setFrame:CGRectMake(0, 0, 30, 30)];
    [cell.textLabel setText:fd.name_];
    return cell;
}
于 2012-05-22T20:12:59.510 回答