我只是试图测试自定义单元格,让表格中的所有单元格都显示“Hi”,但它不起作用......单元格都是空白的。 似乎在我可以设置标签文本之前调用了TheCell类...我不知道如何做到这一点。
TheCell.h
#import <UIKit/UIKit.h> @interface TheCell : UITableViewCell @property (nonatomic, strong) IBOutlet UILabel *arrivalLabel; @endTheCell.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; @endTheCell.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; }更多推荐
发布评论