Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 裁剪到圆形单元格。图像视图?_Ios_Swift_Uiimageview_Tableview_Cell - Fatal编程技术网

Ios 裁剪到圆形单元格。图像视图?

Ios 裁剪到圆形单元格。图像视图?,ios,swift,uiimageview,tableview,cell,Ios,Swift,Uiimageview,Tableview,Cell,无法裁剪单元格。图像视图?以循环 我在很多论坛上都没找到 我对标准单元格(非自定义)使用tableView 我的代码: override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "channelCell", for:

无法裁剪单元格。图像视图?以循环

我在很多论坛上都没找到

我对标准单元格(非自定义)使用tableView

我的代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "channelCell", for: indexPath) 
    cell.accessoryType = .disclosureIndicator
    cell.imageView?.layer.cornerRadius = cell.frame.size.height / 2
    cell.imageView?.clipsToBounds = true
    cell.imageView?.kf.setImage(with: URL(string: channels[indexPath.row].userUrlImage))
    return cell
}
我得到这个图像,检查第二个用户的图像

UPD.


在执行以下操作之前,您应该为图像视图设置一个框架:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "channelCell", for: indexPath) 
    cell.accessoryType = .disclosureIndicator
    cell.imageView?.frame = CGRect(x: 0, y: 0, width: cell.frame.size.height, height: cell.frame.size.height)
    cell.imageView?.contentMode = .scaleToFill
    cell.imageView?.layer.cornerRadius = cell.frame.size.height / 2
    cell.imageView?.clipsToBounds = true
    cell.imageView?.kf.setImage(with: URL(string: channels[indexPath.row].userUrlImage))
    return cell
}

当您填充单元格时,单元格没有对其进行框架布局。
1) 一种方法是调用layoutIfNeeded(),然后设置角半径,等于cell.imageView?.layer.cornerRadius=cell.imageView?.bounds.frame.height/2 但从性能的角度来看,这并不是最好的,因为您创建了冗余的布局过程。
2) 另一种方法是创建UITableViewCell的子类并覆盖它layoutSubviews。然后在布局子视图中设置角半径。
3) 从可重用性的角度来看,最简单也是最好的方法是创建UIImageView的子类并覆盖布局子视图。 下面是第三种方法的代码

/*
Create custom UITableViewCell and use CircledImageView instead of UIImageView.
*/
class CircledImageView: UIImageView {
    override func layoutSubviews() {
        super.layoutSubviews()
        self.layer.cornerRadius = self.bounds.size.height / 2
    }
}

两个单元格的高度不同,所以您可以通过下面的代码设置高度,并获得合适的圆形图像

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 50    // set your cell height
}

好的,有什么问题吗???如果你仔细观察第二个用户,你会发现图像不是圆形的。这更像是一只眼睛划开它,让我知道结果:
cell.imageView?.layer.cornerRadius=cell.imageView!。frame.size.height/2
没有帮助,更新了问题我想我意识到了你的问题。第二个图像的高度不等于它的宽度,请检查它并让我知道