简体   繁体   中英

Can't set properties in Custom UITableViewCell

I made a custom table view cell - i have a header, implementation, and nib. In the nib I set the style to custom, dragged a label on it and made an outlet in the nibs file owner.

From my UITableView Controller I have this code:

static NSString *CellIdentifier = @"adbActivityCell";

adbActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell) {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
    cell = [topLevelObjects objectAtIndex:0];
    //cell =[[adbActivityCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.price.text = [NSString stringWithFormat:@"cell #%d", indexPath.item + 1];

return cell;

If I run this as is XCode tells me that the UITableView Controller is not key value compliant for the label property (the label is named "price"). If I comment out the two lines above and uncomment that one line my application runs, but the label doesn't show up at all, even if I set default text for it.

I've spent quite a lot of time researching tutorials and questions on here with no luck.

Its all about view hierarchy.

You have to add your label outlet to the custom UITableViewCell, because it is the superView of your label in view heirarchy.

That means label is contained in custom cell thats why you have add outlet to custom cell.

self.view->tableView->CustomCellView->UILabel

In your customCell.h file set the IBOutlet of the label. Your problem will be solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM