How to change UITableViewCell Image to Circle in UITableView

前端 未结 11 1505

My code works for the most part.

  • The issue I am having is the images start out as square, and change to circle when I scroll the table or refresh it.

11条回答
  •  心在旅途
    2021-02-12 12:48

    @interface MyCell : UITableViewCell
    @end
    
    @implementation MyCell
    
    - (void)awakeFromNib
    {
        [super awakeFromNib];
        self.imageView.layer.masksToBounds = YES;
        self.textLabel.font = [UIFont systemFontOfSize:17];
        self.textLabel.textColor = [UIColor darkTextColor];
    }
    
    // --- image view frame is empty rect inside tableView: willDisplayCell:
    // +++ set cornerRadius inside layoutSubviews works. 
    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
        // layout image view
        CGRect vfr = self.frame;
        CGRect imgvr = self.imageView.frame;
        imgvr.origin.x = 16;
        imgvr.size.width = CGRectGetHeight(vfr);
        self.imageView.frame = imgvr;
    
        // update corner radius
        self.imageView.layer.cornerRadius = imgvr.size.width * 0.5f;
    
        // layout label
        CGRect lblr = self.textLabel.frame;
        lblr.origin.x = CGRectGetMaxX(imgvr) + 16;
        lblr.size.width = CGRectGetWidth(vfr) - CGRectGetMaxX(imgvr) - 32;
        self.textLabel.frame = lblr;
    }
    
    @end
    

提交回复
热议问题