UILabel在自定义UItableviewcell

时间:2015-07-08 15:21:27

标签: ios objective-c uitableview uilabel tableview

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

  • 单元格样式是自定义
  • Cell具有唯一标识符“profilecell”
  • Cell的类是所需的自定义类型
  • 数据源和委托已连接到View Controller
  • 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“position”
  • 列表项目 - UILabel“special”
  • labelCurrentRole - UILabel题为学校

1 个答案:

答案 0 :(得分:1)

_userProfile.fullNamenilprofilecell.labelNamenil

如果您在界面构建器中创建了单元格,是否正确地将插座连接到labelName