在集合视图单元格中设置文本时出错

Posted

技术标签:

【中文标题】在集合视图单元格中设置文本时出错【英文标题】:Error setting text in collection view cell 【发布时间】:2013-01-11 21:58:54 【问题描述】:

当我将值设置为 NULL 后 nslog 时,为什么?

CustomCollectionVIewCell *cell =[[CustomCollectionVIewCell alloc]init];
NSString *name =[dict valueForKey:@"filterName"];//works
[cell.labelDisplay setText:name];//does not
NSLog(@"name = %@",cell.labelDisplay.text);
cell.isSelected=NO;
[_availableHealthFilters setObject:cell atIndexedSubscript:i];

【问题讨论】:

您的name 显示正确的值吗? 我猜你的名字变量是NULL。尝试记录该字符串。 你在哪里分配/初始化 labelDisplay? 【参考方案1】:

在自定义单元格中初始化您的 labelDisplay。

//customColectionCell.h
@property(strong,nonomatic) NSString *labelText;
-(void)setCellLabelText:(NSString *)labelText;
//customCollectionCell.m
-(id)init
//init cellLabel and addSubView to customCell 


//Than set label in custom method
-(void)setCellLabelText:(NSString *)labelText
_labelText = labelText;
sefl.labelDisplay.text = labelText;

【讨论】:

以上是关于在集合视图单元格中设置文本时出错的主要内容,如果未能解决你的问题,请参考以下文章