错误:在'UITableViewCell'类型的对象上找不到属性'label1'

时间:2014-01-13 23:58:34

标签: ios uitableview label

下面有人有任何想法吗?第6和第6行出现错误。 8从底部。我已经合成了标签。欢呼声。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:NSDateFormatterShortStyle];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}

UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 12, 240, 20)];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(250, 12, 50, 20)];

label1.text = @"Left";
label2.text = @"Right";

[cell.contentView addSubview:label1];
[cell.contentView addSubview:label2];


// Configure the cell...
President *p = (President *)[self.importedRows objectAtIndex:indexPath.row];
cell._label1.text = [NSString stringWithFormat:p.no ];

cell._label2.text = [NSString stringWithFormat: p.name ];


return cell;
}

1 个答案:

答案 0 :(得分:0)

而不是:

cell._label1.text = [NSString stringWithFormat:p.no ];
cell._label2.text = [NSString stringWithFormat: p.name ];

使用:

label1.text = [NSString stringWithFormat:p.no ];
label2.text = [NSString stringWithFormat: p.name ];

您刚刚将它们添加为子视图,但没有单元格的属性。 如果你想像这样封装它,创建你的自定义类来扩展UITableViewCell,而不是这样使用它。