shennis90 shennis90 - 18 days ago 5
iOS Question

Error: property 'label1'not found on object of type 'UITableViewCell'

Anyone got any ideas on the below? The error comes about on lines 6 & 8 from the bottom. I have synthesized the labels. Cheers.

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

Answer

Instead of:

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

use:

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

You just added them as subview but there are not properties of cell. If you want to encapsulate it like that, create your custom class that will extend UITableViewCell and than you can use it this way.