簡體   English   中英

iOS-使用Interface Builder設置UITableView單元格樣式的最佳方法

[英]IOS - Best Way to Style UITableView Cell With Interface Builder

嘿,我已經閱讀了許多有關樣式單元格的教程和文章。 由於界面構建器的樣式功能受到一定限制,因此我不得不嘗試其他方法,並且在試圖找到以編程方式對在界面構建器中創建的UITableViewCell進行編程樣式化的正確方法時,感到矛盾。 一些來源直接在cellForRowAtIndexPath設置單元格的樣式,其他來源建議在.m中為UITableViewCell類進行設置。 cellForRowAtIndexPath樣式時遇到了一些性能問題,因此現在我在.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方法已被覆蓋並正在執行。

如果您要在界面生成器中執行任何操作,則不能使用initWithStyle 請改用awakeFromNib。

自定義UITableViewCell的重寫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM