【问题标题】:UILabel displaying (null) in custom UItableviewcellUILabel 在自定义 UItableviewcell 中显示(空)
【发布时间】:2015-07-08 15:21:26
【问题描述】:

我无法弄清楚为什么我的自定义 TableViewCell 中的 UILabel 在 CellForRowAtIndexPath 中为零。我在另一个视图控制器中使用了与之前的 TableView 相同的方法。这是我已经检查过的内容:

  • 单元格样式是自定义的
  • 单元格具有唯一标识符“profilecell”
  • Cell 的类是所需的自定义类型
  • 数据源和委托都连接到视图控制器
  • 5 个按钮/标签中的每一个都连接到自定义类中各自的属性
  • 已选择动态原型

我可以更改单元格的背景颜色并填充 textLabel.text —— 只是我的UILabels 没有填充。

这里是CellForRowAtIndexPath。这与我的另一个TableVC 完全相同,有效。另外,我不需要实际重用这个自定义单元格,我应该使用不同的方法吗?

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

static NSString *CellIdentifierProfile = @"profilecell";
ProfileSection0TableViewCell *profilecell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierProfile];

     if (profilecell == nil) {
        profilecell = [[ProfileSection0TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifierProfile];
     }

     if (indexPath.section == 0) {
         [profilecell setBackgroundColor:[UIColor colorWithRed:54/255.0f green:61/255.0f blue:147/255.0f alpha:1.0f]];

         if (indexPath.row == 0) {
            profilecell.labelName.text = _userProfile.fullName;
            NSString *test = [NSString stringWithFormat:@"%@", profilecell.labelName.text];
            NSLog(@"text %@", test); //shows that label is (null)

            profilecell.labelName.textColor = [UIColor whiteColor];

            //profilecell.labelSpecialty.text = _userProfile.specialty;
            //profilecell.labelSpecialty.textColor = [UIColor whiteColor];

        }
    }
    return profilecell;
}

非常感谢。

.h 文件:

    @interface ProfileSection0TableViewCell : UITableViewCell
    @property (nonatomic, weak) IBOutlet UILabel* labelName;
    @property (nonatomic, weak) IBOutlet UILabel* labelSpecialty;
    @property (nonatomic, weak ) IBOutlet UILabel* labelCurrentRole;
    @property (nonatomic, weak) IBOutlet UILabel* labelPosition;
    @property (nonatomic, weak) IBOutlet UIButton* facePicture;
    @end

.m 文件:

    @implementation ProfileSection0TableViewCell
    @synthesize  labelName, labelSpecialty, labelPosition, facePicture, labelCurrentRole;

    @end

编辑:

看起来 ProfileSection0TableViewCell 分配正确,但单元格内的成员变量为零。在网点中,连接如下:

  • facePicture - 按钮
  • labelName- UILabel "person"
  • labelPosition - UILabel“位置”
  • 列表项 - UILabel“专业”
  • labelCurrentRole - UILabel 标题为学校

【问题讨论】:

  • 变量_userProfile.fullName 的值怎么样?您能分享一下您如何在自定义UITableViewCellProfileSection0TableViewCell 中设置UILabel labelName我>
  • _userProfile.fullName 不为空——它在对象中包含所有正确的数据
  • 根据您在NSLog 之后的评论,您的测试表明名为testNSString *nil,而不是单元格的labelName 标签是nil。你需要更深入地挖掘。您确定 profileCell 不也是 nil 吗?细胞的 IBOutlets 怎么样?它们连接正确吗?
  • @Aaron IBOutlets 都很好。 profileCell 已初始化,但 labelName 从未设置。我有可能以某种方式覆盖数据吗?

标签: ios objective-c uitableview uilabel tableview


【解决方案1】:

_userProfile.fullNamenilprofilecell.labelNamenil

如果您在界面生成器中制作了单元格,您是否正确地将插座连接到labelName

【讨论】:

  • 我同意,你如何设置_userProfile.fullName?这似乎是您代码中的薄弱环节,没有看到该变量的设置位置。
  • _userProfile 是 TVC 中的一个属性,由 jSON 数据填充,当我使用调试器时,fullname 中包含正确的文本。我还能够毫无问题地将 cell.TextLabel.text 设置为 _userProfile.fullName
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多