Ios 我无法在TableViewCell中设置标签
我只是想通过让表中的所有单元格都说“嗨”来测试自定义单元格,但它不起作用。。。这些单元格都是空白的。似乎在我可以设置标签文本之前调用了TheCell类。。。我不知道还能怎么做 cell.hIos 我无法在TableViewCell中设置标签,ios,uitableview,Ios,Uitableview,我只是想通过让表中的所有单元格都说“嗨”来测试自定义单元格,但它不起作用。。。这些单元格都是空白的。似乎在我可以设置标签文本之前调用了TheCell类。。。我不知道还能怎么做 cell.h #import <UIKit/UIKit.h> @interface TheCell : UITableViewCell @property (nonatomic, strong) IBOutlet UILabel *arrivalLabel; @end TableViewControll
#import <UIKit/UIKit.h>
@interface TheCell : UITableViewCell
@property (nonatomic, strong) IBOutlet UILabel *arrivalLabel;
@end
TableViewController.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.arrivalLabel.frame = CGRectMake(0, 0, 320, 30);
[self.contentView addSubview:self.arrivalLabel];
}
return self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"anIdentifier";
TheCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[TheCell alloc] init];
cell.arrivalLabel.text = @"hi";
return cell;
}
你有一些问题。您正在调用cell类的
init
方法。您应该调用initWithStyle:reuseIdentifier:
方法
您还应该仅在调用dequeue…
返回nil
时分配单元格
然后,您需要确保单元格的initWithStyle:reuseIdentifier:
方法正在为arrivalLabel
属性创建标签实例
TheCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[TheCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.arrivalLabel.text = @"hi";
另一个大问题是单元格的initWithStyle:reuseIdentifier:
方法。您永远不会创建标签
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_arrivalLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
[self.contentView addSubview:_arrivalLabel];
}
return self;
}
你有一些问题。您正在调用cell类的
init
方法。您应该调用initWithStyle:reuseIdentifier:
方法
您还应该仅在调用dequeue…
返回nil
时分配单元格
然后,您需要确保单元格的initWithStyle:reuseIdentifier:
方法正在为arrivalLabel
属性创建标签实例
TheCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[TheCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.arrivalLabel.text = @"hi";
另一个大问题是单元格的initWithStyle:reuseIdentifier:
方法。您永远不会创建标签
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_arrivalLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
[self.contentView addSubview:_arrivalLabel];
}
return self;
}
这是不正确的:
TheCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[TheCell alloc] init];
您必须执行以下操作:
TheCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[TheCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
这是不正确的:
TheCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[TheCell alloc] init];
您必须执行以下操作:
TheCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[TheCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
您的
cell
都是通过代码还是通过xib完成的?都是通过编程完成的。。。我尝试不使用xib或故事板。您的标签是在单元格的init中创建的?您的TheCell
都是在代码中完成的还是通过xib完成的?都是以编程方式完成的。。。我试着不使用xib或故事板。你的标签是在单元格的init中创建的?单元格仍然显示为空白。。。我试图添加self.arrivalLabel.text=@“TEST”
到cell.m,它仍然不会显示。不,文本的设置是它应该在的位置。用initWithStyle:reuseIdentifier:
方法的代码更新您的问题。我已经在问题中找到了方法代码。。。我没有改变它。顺便说一句,谢谢你的帮助!哎呀,你就是这么做的。这就是问题所在。您永远不会为arrivalLabel
创建标签。有两件事:1)不要将其设置为IBOutlet
,因为这是在代码中完成的。2) 该属性实际上并不分配实例。您仍然需要调用alloc
init…`来创建属性访问的实际实例。单元格仍然显示为空白。。。我试图添加self.arrivalLabel.text=@“TEST”
到cell.m,它仍然不会显示。不,文本的设置是它应该在的位置。用initWithStyle:reuseIdentifier:
方法的代码更新您的问题。我已经在问题中找到了方法代码。。。我没有改变它。顺便说一句,谢谢你的帮助!哎呀,你就是这么做的。这就是问题所在。您永远不会为arrivalLabel
创建标签。有两件事:1)不要将其设置为IBOutlet
,因为这是在代码中完成的。2) 该属性实际上并不分配实例。您仍然需要调用alloc
init…`来创建属性访问的实际实例。单元格仍然显示为空白。。。我试图添加self.arrivalLabel.text=@“TEST”
到单元格.m
但它仍然不会显示。单元格仍然显示为空白。。。我试图添加self.arrivalLabel.text=@“TEST”
到cell.m,它仍然不会显示。