我无法在TableViewCell中设置标签(I am unable to set a label in TableViewCell)

编程入门 行业动态 更新时间:2024-10-28 02:21:16
我无法在TableViewCell中设置标签(I am unable to set a label in TableViewCell)

我只是试图测试自定义单元格,让表格中的所有单元格都显示“Hi”,但它不起作用......单元格都是空白的。 似乎在我可以设置标签文本之前调用了TheCell类...我不知道如何做到这一点。

TheCell.h

#import <UIKit/UIKit.h> @interface TheCell : UITableViewCell @property (nonatomic, strong) IBOutlet UILabel *arrivalLabel; @end

TheCell.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; }

TableViewController.m

- (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; }

I'm just trying to test out the custom cell by have all the cells in the table say "Hi", but it won't work... the cells are all blank. It seems that the TheCell class is called before I can set the label text... I don't know how else to do it.

TheCell.h

#import <UIKit/UIKit.h> @interface TheCell : UITableViewCell @property (nonatomic, strong) IBOutlet UILabel *arrivalLabel; @end

TheCell.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; }

TableViewController.m

- (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; }

最满意答案

你有一些问题。 您正在调用您的单元类的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; }

You have a few issues. You are calling the init method of your cell class. You should be calling the initWithStyle:reuseIdentifier: method instead.

You should also only allocate the cell if the call to dequeue... returns nil.

Then you need to make sure that your cell's initWithStyle:reuseIdentifier: method is creating the label instance for the arrivalLabel property.

TheCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!cell) { cell = [[TheCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.arrivalLabel.text = @"hi";

The other big problem is in your cell's initWithStyle:reuseIdentifier: method. You never create the label.

- (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; }

更多推荐

本文发布于:2023-07-24 10:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1244911.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标签   TableViewCell   unable   label   set

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!