1

嘿,我已经阅读了许多关于样式单元格的教程和文章。由于界面构建器的样式功能有些有限,我被迫研究其他方法,并且在尝试找到以编程方式对界面构建器中创建的 UITableViewCell 进行样式设置时感到矛盾。一些来源直接在cellForRowAtIndexPath其他来源中对单元格进行样式设置,建议在 UITableViewCell 类的 .m 中进行。我在进行样式设置时遇到了一些性能问题,cellForRowAtIndexPath所以现在我正在为 classes .m 文件中的所有内容设置样式。这是样式的代码:

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

这是我创建每个 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;
}

现在由于某种原因,上面的代码实际上不起作用,我只成功地直接在 cellForRowAtIndexPath 委托方法中设置单元格的样式。因此,在尝试解决此问题之前,我想知道,我应该将使用<QuartzCore/QuartzCore.h>和其他无法由界面生成器编辑的库的单元格样式放在哪里?如果正确的答案是我目前正在研究的答案,那么为什么样式没有出现?“测试!” 出现在每个创建的单元格中,因此我知道该initWithStyle方法已被覆盖并正在执行。

4

1 回答 1

6

如果您在界面生成器中执行任何操作,则无法使用initWithStyle. 请改用 awakeFromNib。

覆盖自定义 UITableViewCell

于 2013-08-15T20:37:05.523 回答