Ala*_*lan 2 objective-c uitableview ios
我正在使用TableView xib,所有委托和数据源似乎运行正常.如果我设置self.textLabel.text,它会正确显示通用的tableviews数,但我需要显示自定义的TableViewCell.
我创建了一个只包含tableviewcell的HistoryCell.xib.我创建了一个UITableViewCell类"HistoryCell.h/HistoryCell.m",它被设置为HistoryCell.xib的文件所有者.我将UILabels连接到HistoryCell.h
UILabel statusLabel
UILabel nameLabel
UILabel timeLabel
在我的主ViewController类中他 cellForRowAtIndexPath
我正在投入
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"historyCellType";
HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[HistoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.nameLabel.text = @"Donald Duck";
return cell;
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
谢谢!
您应该像这样注册nib(可能在viewDidLoad中):
[self.tableView registerNib:[UINib nibWithNibName:@"HistoryCell" bundle:nil ] forCellReuseIdentifier:@"historyCellType"];
Run Code Online (Sandbox Code Playgroud)
然后在您的cellForRowAtIndexPath方法中,使用这种新方法,您不需要if cell == nil子句:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"historyCellType" forIndexPath:indexPath];
cell.nameLabel.text = @"Donald Duck";
return cell;
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
3508 次 |
| 最近记录: |