简体   繁体   中英

IOS - Best Way to Style UITableView Cell With Interface Builder

Hey I have read a number of tutorials and articles on styling cells. Due to the fact that the styling capabilities of interface builder are somewhat limited I have been forced to research other methods and am left with conflicting feelings when trying to find the proper way to programatically style a UITableViewCell created in interface builder. Some of the sources style the cells directly in the cellForRowAtIndexPath others suggest doing it in the .m for the UITableViewCell class. I ran into some performance issues when styling in cellForRowAtIndexPath so now I am styling everything in the classes .m file. Here is the code for the styles:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self)
    {
        NSLog(@"test!");

        _nameLabel.font = [UIFont fontWithName:@"Gotham Light" size:16];
        _locationLabel.font = [UIFont fontWithName:@"Gotham Light" size:14];
        _titleLabel.font = [UIFont fontWithName:@"Gotham Light" size:12];

        [_thumbnailUserImage.layer setCornerRadius:24];
        [_thumbnailUserImage.layer setBorderWidth:2];
        [_thumbnailUserImage.layer setBorderColor:[[UIColor whiteColor] CGColor]];
        [_thumbnailUserImage.layer setMasksToBounds:YES];
        self.contentView.backgroundColor = [UIColor colorWithRed:(247/255.0) green:(245/255.0) blue:(242/255.0) alpha:1];
    }

    return self;
}

Here is where I create each cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    User *user;
    ContactsCell *cell = [[ContactsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell = (ContactsCell *)[tableView dequeueReusableCellWithIdentifier:@"ContactsCell"];
    user = [users objectAtIndex:indexPath.row];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ContactsCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.nameLabel.text = [NSString stringWithFormat: @"%@ %@", user.firstName, user.lastName];
    cell.titleLabel.text = [NSString stringWithFormat:@"%@", user.position];
    cell.locationLabel.text = [NSString stringWithFormat:@"%@", user.location];

    [cell.thumbnailImageView setImageWithURL:[NSURL URLWithString: user.profileUrl] placeholderImage:[UIImage imageNamed:@"stockProfilePhoto.png"]];

    return cell;
}

Now for some reason the above code does NOT actually work , I have only been successful with styling the cells directly in the cellForRowAtIndexPath delegate method. So I was wondering before I try to solve this issue, where should I be putting cell styles that use the <QuartzCore/QuartzCore.h> and other libraries that can't be edited by interface builder? If the correct answer is the one I am currently working on, then how come the styles aren't showing up? "Test!" appears for every created cell so I know the the initWithStyle method has been overwritten and is executing.

If you are doing anything in interface builder you can't use initWithStyle . Use awakeFromNib instead.

override for custom UITableViewCell

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