【问题标题】:UIImageView outlet of Custom TableViewCell nil自定义 TableViewCell 的 UIImageView 出口 nil
【发布时间】:2014-10-08 08:46:17
【问题描述】:

我在情节提要上创建了一个UITableViewCell 原型单元,然后我将它链接到我的ImageViewCell,它继承自UITableViewCell。我为原型单元格上的每个控件创建了IBOutlet,它包含一些UILabel 和一个UIImageView。当我尝试将数据填充到这些控件中时,UILabel 运行良好,但UIImageView 始终为零并且不会显示任何图像。代码:

@interface ImageTableViewCell : UITableViewCell
@property (readonly, nonatomic, strong) Post *post;

// set value for post
- (void)setPost:(Post *)post;
@end

@implementation ImageTableViewCell
{
    __weak IBOutlet UILabel *titleLabel;
    __weak IBOutlet UILabel *descriptionLabel;
    __weak IBOutlet UILabel *pointLabel;
    __weak IBOutlet UIImageView *imageView;

}
- (void)setPost:(Post *)post
{
    _post = post;

    // update ui
    titleLabel.text = self.post.title;
    descriptionLabel.text = self.post.descriptions;
    pointLabel.text = self.post.point;    

    [imageView setImage:[UIImage imageNamed:@"mylocalimage.png"]];

}
@end

在我的控制器中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(feedData == nil || [feedData count] == 0)
    {
        return nil;
    }

    NSDictionary *postData = [feedData objectAtIndex:indexPath.row];

    // prepare data to pass to table view cell
    Post *post = [[Post alloc] init];    
    post.title = [postData objectForKey:@"title"];
    post.descriptions = [postData objectForKey:@"description"];
    post.total_likes = [postData objectForKey:@"total_likes"];
    post.total_shares = [postData objectForKey:@"total_shares"];
    post.image = [postData objectForKey:@"image"];

    ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ImageCell"];;
    if(cell == nil)
    {
        cell = [[ImageTableViewCell alloc] init];
    }

    [cell setPost:post];


    return cell;
}

所有UILabel 工作良好,但UIImageView 的图像从未设置,我尝试用本地图像设置但仍然无法正常工作,经过一番调查,我发现当时我设置imageView 的图像,它始终为零。那么我该如何克服呢?

更新

链接图片

【问题讨论】:

  • Whats sd_setImageWithURL 看起来您正在尝试下载带有 nil 占位符的图像?您是否检查过此功能是否按预期工作
  • 如上所述,我尝试使用本地图像进行设置,但仍然无法正常工作
  • 您是否忘记在 Interface Builder 中链接 IBOutlet?
  • 是的,我从情节提要中拖放它,我不认为我的链接有任何问题,因为UILabel 工作正常,只有UIImageView 始终为零
  • @pe60t0 实际上可以。能否请您发布一个答案,并简要解释为什么变量名在这里很重要,以便我可以接受您的答案

标签: ios objective-c uitableview uiimageview


【解决方案1】:

尝试将UIImageView 的变量名称更改为“imageView”以外的名称。原因是UITableViewCell 本身有一个名为“imageView”的属性。因此,当您设置属性时,它必须尝试设置未链接到您的插座的超类属性,因此结果。

【讨论】:

  • 非常感谢,你拯救了我的一天 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
  • 2016-08-04
相关资源
最近更新 更多