ios - 如何从 initWithCode 为自定义 Cell 设置 UIImageView 的图像属性?

标签 ios objective-c uitableview

我该怎么做?

如果我这样设置,它会完美运行:

if(indexPath.row == lastSelectedCell) {
    // last cell
    TWMainViewExpandedCell *cell = (TWMainViewExpandedCell *)[tableView dequeueReusableCellWithIdentifier:ExpandedCellIdentifier];

    if (cell == nil) {

        cell = (TWMainViewExpandedCell *)[[[NSBundle mainBundle] loadNibNamed:@"MainViewExpandedCell" owner:self options:nil] objectAtIndex:0];
        _circleImage.image = [UIImage imageNamed:@"circle"];

    }

    return cell;
}

但是,当尝试从此处设置相同的图像属性时(顺便说一句,这是 UITableViewCell 的子类):

@implementation TWMainViewExpandedCell

-(id)initWithCoder:(NSCoder *)aDecoder
{
    NSLog(@"initWithCoder");

    self = [super initWithCoder: aDecoder];
    if (self) {
        _circleImage.image = [UIImage imageNamed:@"circle"];
    }
    return self;
}

最后一个不起作用。为什么?我知道正在调用 initWithCoder。

最佳答案

调用 initWithCoder: 时,您的 circleImage socket 尚未连接,因此它仍然是 nil。在 awakeFromNib 中执行此操作。

关于ios - 如何从 initWithCode 为自定义 Cell 设置 UIImageView 的图像属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17690260/

相关文章:

ios - Objective-C - 混合协议(protocol)和类别

objective-c - 除非应用程序当前正在运行,否则将文件拖放到应用程序图标上不起作用?

ios - 常见 iOS 应用程序的 URL 方案

ios - 我如何获得 CPU、GPU、使用的磁盘、Apple 设备中的免费信息

objective-c - objective-c 中的所有属性的 setter/getter 都一样吗?

ios - 如何在 UIATablecell 中访问图像

iphone - 着色UIBezierPath

swift - 快速点击栏按钮上的退出键盘

ios - 错误的 UITableViewCell 高度与 UITableViewAutomaticDimension

ios - 为什么 dequeueReusableCellWithIdentifier 会导致我的整个应用程序崩溃/挂起?