简体   繁体   English

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

[英]how to set initial value of table view cell property in iOS

I have an image property on my table view cell. 我在表格视图单元格上有一个图像属性。 I want to populate this image to an image. 我想将此图像填充为图像。

My cell looks like: 我的手机看起来像:

//.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

This doesn't set the image. 这不会设置图像。 How can I set the image for this cell within the cell class? 如何在单元格类中为此单元格设置图像?

Since your image view is an IBOutlet, I'm assuming you've got your cell layout defined in a .xib or storyboard. 由于您的图像视图是IBOutlet,因此我假设您已在.xib或情节提要中定义了单元格布局。 That being the case, your initialization code should go in the awakeFromNib method. 在这种情况下,您的初始化代码应该放在awakeFromNib方法中。 This is the method that is called when the cell is created from the nib. 这是从笔尖创建单元格时调用的方法。 That's why the //Initialization code comment is there. 这就是//Initialization code注释在那里的原因。 initWithStyle:reuseIdentifier: is never called in this scenario, which is why your image is not appearing. 在这种情况下,永远不会调用initWithStyle:reuseIdentifier: ,这就是为什么图像不出现的原因。

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

Otherwise if you have a Nib or Storyboard you're using then set the default in the prototype cell you are using. 否则,如果您使用的是Nib或Storyboard,则在使用的原型单元中设置默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM