简体   繁体   中英

Custom UITableViewCell - nothing visible

I have UITableViewCell class, which contains customLabel and imageView. When I try to load them in my main UITableViewController , nothing happens.

Main UITableViewController contains:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

[cell.customButton setTitle:@"Test" forState:UIControlStateNormal];

PFUser *user = [self.members objectAtIndex:indexPath.row];
cell.customLabel.text = [user objectForKey:@"Name"];

PFFile *userImage = [user objectForKey:@"Image"];
[userImage getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
    if (!error) {
        cell.imageView.image = [UIImage imageWithData:data];
        [cell setNeedsLayout];
    }
}];
return cell;

}

UserTableViewCell contains:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    self.customLabel = [[UILabel alloc] initWithFrame:CGRectMake(3, 5, 165, 30)];
    self.customLabel.font = [UIFont systemFontOfSize:14];
    self.customLabel.textColor = [UIColor blackColor];
    self.customLabel.backgroundColor = [UIColor clearColor];
    self.customLabel.highlightedTextColor = [UIColor whiteColor];
    self.customLabel.adjustsFontSizeToFitWidth = YES;
    [self.contentView addSubview:self.customLabel];

    self.customButton = [[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:CGRectMake(180, 5, 40, 30)];
    [self.customButton addTarget:self action:@selector(logButtonRow:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.customButton];

    self.imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(60, 1, 50, 50)];
    [self.contentView addSubview:self.imageView2];

}
return self;

}

Have I missed something? I've set Prototype cell identifier to "Cell" and custom class to UserTableViewCell (is this required?)

It appears that you set up your cell in IB, and, if so, initWithStyle:reuseIdentifier: will not be called. If you want to add UI elements to your cell in code, then you should implement initWithCoder instead. Alternatively, you could register your class (in viewDidLoad of your table view controller), and that will cause initWithStyle:reuseIdentifier: to be called (the cell in IB would then be superfluous, since the table view will get the cell from your class definition).

[self.tableView  registerClass:[UserTableViewCell class] forCellReuseIdentifier:@"Cell"]; 

You need to alloc and init your cell when its called the first time:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"Cell";
  UserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

  if(cell == nil)
  {
    // initialize your cell here
    cell = ...
  }

...

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