我正在创建 UITableViewCell
子类,它需要保存 3 种类型的图像:facebook twitter 和 linkedin。问题是我需要知道单元格目前支持哪种服务,所以我尝试向名为 service
的类添加一个属性,它是 NSString
。
这就是我设置单元格以将其添加到表格中的方式:
static NSString *sCellIdentifier = @"sCell";
SocialTableViewCell *sCell = [tableView dequeueReusableCellWithIdentifier:sCellIdentifier];
sCell.service = @"service";
if (sCell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ExampleView" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[SocialTableViewCell class]])
{
sCell = (SocialTableViewCell *)currentObject;
break;
}
}
}
if (indexPath.row == 0) {
sCell.serviceImage.image = [UIImage imageNamed:@"icon_Twitter.png"];
sCell.service = @"twitter";
return sCell;
}
我正在尝试像这样设置属性:
sCell.service = @"twitter";
当我尝试从 SocialTableViewCell
类中的 awakeFromNib
方法读取它时,我得到 null。我应该怎么做才能解决这个问题?
最佳答案
awakeFromNib
方法在您设置 service
属性之前被调用。
您可以使用 service
属性的 setter 方法:
-(void) setService:(NSString *)service {
// Set the value to the internal iVar
_service = service;
// Here you can do what you want with the service value like
self.titleLabel.text = service;
}
关于iphone - 向 UITableView 子类添加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18352590/