如何在iOS中设置表视图单元属性的初始值

时间:2014-12-18 20:52:04

标签: ios objective-c uitableview

我的表格视图单元格中有一个图像属性。我想将此图像填充到图像中。

我的手机看起来像:

//.h
@interface GAFriendStatusTableViewCell : PFTableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *friendImage;

@end

//.m
#import "GAFriendStatusTableViewCell.h"

@implementation GAFriendStatusTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.friendImage.image = [UIImage imageNamed:@"user.png"];
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

@end

这不会设置图像。如何在单元格类中设置此单元格的图像?

2 个答案:

答案 0 :(得分:3)

由于您的图片视图是IBOutlet,我假设您已在.xib或故事板中定义了单元格布局。在这种情况下,您的初始化代码应该采用awakeFromNib方法。这是从nib创建单元格时调用的方法。这就是//Initialization code评论的原因所在。在这种情况下永远不会调用initWithStyle:reuseIdentifier:,这就是您的图像没有出现的原因。

答案 1 :(得分:0)

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

中设置

否则,如果您使用的是Nib或Storyboard,请在您使用的原型单元格中设置默认值。