简体   繁体   English

单元格视角的半径

[英]corner radius for cells view

I am using tablet view and my table view is grouped.I use single prototype cell. 我正在使用平板电脑视图,我的表视图已分组。我使用单个原型单元格。 In my cell, I use UIView and want to make corner only first cell view top and last cell view bottom. 在我的单元格中,我使用UIView并且想要只在角落的第一个单元格视图顶部和最后一个单元格视图底部。 I use this code below. 我在下面使用此代码。 when table view first loading its only show first cell view left corner but not for right and bottom cell view.when I scrolling last and come back first it's working correctly. 当表视图首次加载时,它只显示第一个单元格视图左角,但不显示右下角单元格视图。当我最后滚动并返回时,它正常工作。 Need help pls. 需要帮助请。 Thank you and Sorry for my bad English 谢谢,抱歉我的英语不好

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var tableview: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension ViewController:UITableViewDataSource,UITableViewDelegate{

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        // tableview section
        let cell = tableView .dequeueReusableCell(withIdentifier: "cell") as! MyTableViewCell
        if indexPath.row == 0{

            cell.myview.roundCorners(corners: [.topLeft,.topRight], radius: 10)

        } else if indexPath.row ==  3 {
            cell.myview.roundCorners(corners: [.bottomRight,.bottomLeft], radius: 10)

        }

        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 200
    }


    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

       // let cell = cell as! MyTableViewCell
    }
}

extension UIView {
    func roundCorners(corners:UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath

        self.layer.mask = mask
    }
}

You should also handle rows in the middle like this: 您还应该像这样处理中间的行:

switch indexPath.row {
case 0: //round top
    cell.myview.roundCorners(corners: [.topLeft,.topRight], radius: 10)
case 3: //round bottom
    cell.myview.roundCorners(corners: [.bottomRight,.bottomLeft], radius: 10)
default: //remove rounded corners. This is important since cells are reused
    cell.myview.roundCorners(corners: [.allCorners], radius: 0)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM