【问题标题】:CustomTableViewCell Properties aren't being set (they are nil)未设置 CustomTableViewCell 属性(它们为零)
【发布时间】:2015-05-01 07:53:45
【问题描述】:

我有一个customTableViewCell,我正在尝试设置图像和一些文本。单元格创建成功,但属性为nil。我不确定.xib 中是否缺少某些东西,但我就是想不通。网点设置在customViewCell.h,连接在.xib

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  static NSString *cellIdentifier = @"socialMediaCell";    

  CustomSocialMediaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

  CustomSocialMediaTableViewCell *tagCell = (CustomSocialMediaTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"socialMediaCell"];

  if (cell == nil) {

      cell = [[CustomSocialMediaTableViewCell alloc] 
        initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

      tagCell = [[CustomSocialMediaTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  }

  NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"socialPosts"];

  UIImage* image = [UIImage imageWithData:imageData];

  cell.socialMediaTextView.text = @"TEXT";

  cell.socialMediaImageView.image = image;

  return cell;
}

【问题讨论】:

  • 确保您的网点是@Property..
  • 请更正有问题的代码格式...
  • cellIdentifier = @"socialMediaCell" OR cellIdentifier = @"CustomSocialMediaTableViewCell" ?
  • 您要使用哪个标识符?还是你把它放在那里说你都试过了?
  • 另外你为什么要为单元格创建一个.xib?创建一个 UITableviewCell 类,然后在情节提要中选择 tableview 单元格并将其类更改为CustomSocialMediaTableViewCell。将标签和图像拖放到类 .h 文件中。

标签: ios objective-c iphone uitableview uiimage


【解决方案1】:

你引用了同一个 CustomTableViewCell 两次,然后你给它分配了东西:

static NSString *cellIdentifier = @"socialMediaCell";    

****Declared once here****
CustomSocialMediaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

****Declared a second time here***
CustomSocialMediaTableViewCell *tagCell = (CustomSocialMediaTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"socialMediaCell"];

使用第一个声明的cell 并删除第二个。

然后你说如果单元格是空的,设置样式你已经这样做了两次。做一次。

if (cell == nil) {

  cell = [[CustomSocialMediaTableViewCell alloc] 
    initWithStyle:UITableViewCellStyleDefault];
}

然后你可以添加和分配你需要的东西。

我有一种感觉,当您只需在情节提要中选择 TableViewCell 并设置它的类时,您可能已经尝试通过现代方法创建一个 .xib 来以更困难的方式执行此操作。然后你可以 ctrl + 从你的 UILabel 和 UIImage 拖到你的 .h 文件中。

如果您不希望这样做或没有以这种方式构建它,请确保您引用的 UILabel 和 UIImage 已正确连接,并且您已为两者声明 IBOutlet - 就像这样

@property (nonatomic, weak) IBOutlet UILabel * customText; 

【讨论】:

  • 我刚刚声明了两个单独的变量,以显示我尝试过的不同方式,但没有成功。是的,我的 CustomCell 确实有一个单独的 .xib,但无论哪种方式,我仍然对为什么创建单元格但属性为零 @SASmith 感到困惑
【解决方案2】:

使用以下代码在if (cell==nil) 内编辑

    if (cell == nil) { //Check cell is nil or not


   NSArray *nib;
   nib = [[NSBundle mainBundle] loadNibNamed:@"CustomSocialMediaTableViewCell"
                                                owner:self options:nil]; //if cell is nil add CustomSocialMediaTableViewCell Xib

   for (id oneObject in nib) if ([oneObject isKindOfClass:[CustomSocialMediaTableViewCell class]])
                cell = (CustomSocialMediaTableViewCell *)oneObject;

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-03
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 2013-01-18
    • 1970-01-01
    相关资源
    最近更新 更多