UITableViewCell 上的圆形 UIImageView 在首次加载时不起作用

Posted

技术标签:

【中文标题】UITableViewCell 上的圆形 UIImageView 在首次加载时不起作用【英文标题】:Circular UIImageView on UITableViewCell not working on first load 【发布时间】:2015-12-20 20:51:32 【问题描述】:

在我的应用程序的消息部分,我有一个表格视图,用于加载说它的用户的文本和图像。我想要图像的圆形图像视图,并且已经编写了代码来执行此操作,但是当我第一次加载它时,可见单元格仍然有一个方形图像。这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath


    UITableViewCell *cell = [self.messagesTable dequeueReusableCellWithIdentifier:@"messageCell"];

    if (!cell) 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"messageCell"];

    

    cell.textLabel.text = [messagesArray objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:15.0f];
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cell.textLabel.numberOfLines = 0;

    cell.imageView.image = [UIImage imageWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"kUserImageData"]];

    cell.imageView.layer.cornerRadius = cell.imageView.bounds.size.width / 2;
    cell.imageView.clipsToBounds = YES;

    NSLog(@"%f", cell.imageView.bounds.size.height);

   return cell;

此外,奇怪的是前几个单元格(具有方形图像)的 NSLog 为 0.000000。我真的不知道我做错了什么。

【问题讨论】:

【参考方案1】:

试试这个代码:

cell.imageView.layer.cornerRadius = cell.imageView.bounds.size.width / 2;

imageView.layer.masksToBounds = YES;

希望这会奏效。

【讨论】:

以上是关于UITableViewCell 上的圆形 UIImageView 在首次加载时不起作用的主要内容,如果未能解决你的问题,请参考以下文章

UITableViewCell 中的圆形按钮

UITableViewCell 圆形图像视图

按 UITableViewCell 时,圆形视图消失了

UITableViewCell 的子视图不能是圆形的

更改 UITableViewCell 中圆形编辑按钮的颜色

圆形 UITableViewCell 仅底角或顶角,但它不起作用 Swift iOS 10