自定义UITableViewCell
问题描述:
我已经实现了一个自定义表格视图单元格。我可以加载单元格,这不是问题。我正在努力的是设置标签的文本属性。我的代码在下面,我做错了什么?自定义UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[self.icaoLabel setTag: 1];
[self.nameLabel setTag: 2];
[self.tafLabel setTag: 3];
[self.metarLabel setTag: 4];
static NSString *CellIdentifier = @"customCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed: @"TAFandMETARCustomCell" owner: self options: nil];
cell = customCell;
self.customCell = nil;
}
// Configure the cell...
TAFandMETAR *taf = (TAFandMETAR *)[tafAndMETARs objectAtIndex: indexPath.row];
//cell.textLabel.text = [dateFormatter stringFromDate: [taf creationDate]];
UILabel *label;
label = (UILabel *)[cell viewWithTag: 1];
label.text = [taf icaoCode];
label = (UILabel *)[cell viewWithTag: 2];
label.text = [taf airfieldName];
label = (UILabel *)[cell viewWithTag: 3];
label.text = [taf taf];
label = (UILabel *)[cell viewWithTag: 4];
label.text = [taf metar];
return cell;
}
谢谢。
答
假设您的标签是您的自定义单元格的子视图并存在于单元格的nib文件中,那么为它们设置标签的最简单方法是直接在IB中执行此操作 - 选择IB中的每个标签并将其标签设置在检查器窗口中。
谢谢 - 我之前没有发现标签选项。接下来的问题 - 它只在您选择单元格时显示数据 - 否则它是空白的。此外,原始标签文本(所以我可以识别笔尖上的每个标签)只是写在最上面,所以有两个不同的文本。我做错了什么? – 2010-09-17 23:36:45
我不确定我做错了什么,但在IB中重新创建单元格已解决此问题。 – 2010-09-18 10:55:09