iphone - 向 UITableView 子类添加属性

标签 iphone ios objective-c cocoa-touch uitableview

我正在创建 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/

相关文章:

ios - 使用 JS 从 IOS 打印 PDF

iphone - 我想在持续时间达到 5 分钟时停止 AVAudioRecorder

iphone - 如何为 Cydia 商店构建应用程序?

iphone - 如何在应用程序初始化之前获取 GPS 位置?

ios - 禁用硬件键盘编辑 UITextField

ios - 通过键盘应用程序或 iMessage 应用程序截取屏幕截图

iphone - Xcode 4.6 构建、运行并立即完成(模拟器 iOS 6.1)

objective-c - 获取与释放实例相关的崩溃

iphone - 未收到 UIMoviePlayerControllerWillExitFullscreenNotification

iphone - ABRecordRef vCard