How to change UITableViewCell Image to Circle in UITableView

前端 未结 11 1472

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:36

    Most likely the cell's frame (and hence the imageView's frame) isn't set the very first time the cell is created. So setting the imageView's layer's cornerRadius based on that initial frame doesn't work. The best solution is to implement the tableView:willDisplayCell:forRowAtIndexPath: delegate method and set the layer there:

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        cell.imageView.layer.cornerRadius = cell.imageView.frame.size.width / 2.0;
    }
    

提交回复
热议问题