【发布时间】: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的值怎么样?您能分享一下您如何在自定义UITableViewCell类ProfileSection0TableViewCell 中设置UILabellabelName我> -
_userProfile.fullName 不为空——它在对象中包含所有正确的数据
-
根据您在
NSLog之后的评论,您的测试表明名为test的NSString *是nil,而不是单元格的labelName标签是nil。你需要更深入地挖掘。您确定profileCell不也是nil吗?细胞的 IBOutlets 怎么样?它们连接正确吗? -
@Aaron IBOutlets 都很好。 profileCell 已初始化,但 labelName 从未设置。我有可能以某种方式覆盖数据吗?
标签: ios objective-c uitableview uilabel tableview