需要實(shí)現(xiàn)這樣如下效果:
image.png
主要方法如下:
func SetMutiBorderRoundingCorners(_ rectCorner: UIRectCorner, corner: CGFloat) {
// [UIRectCorner.bottomLeft, UIRectCorner.topRight]
let maskPath = UIBezierPath.init(roundedRect: self.bounds,
byRoundingCorners: rectCorner,
cornerRadii: CGSize(width: corner, height: corner))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.bounds
maskLayer.path = maskPath.cgPath
self.layer.mask = maskLayer
}
定義一個(gè)枚舉
enum RedMyPageCellUIPostion {
case top // 頂部
case center // 中間
case bottom // 底部
}
根據(jù)枚舉值來(lái)設(shè)置cell的圓角
var cellPosition :RedMyPageCellUIPostion = .center {
didSet {
switch cellPosition {
case .top:
self.bgView.SetMutiBorderRoundingCorners([.topLeft, .topRight], corner: 10.0)
case .center:
self.bgView.SetMutiBorderRoundingCorners([.allCorners], corner: 0.0)
case .bottom:
self.bgView.SetMutiBorderRoundingCorners([.bottomLeft, .bottomRight], corner: 10.0)
}
}
}
在列表數(shù)據(jù)源處,根據(jù)數(shù)據(jù)源設(shè)置cell的枚舉值
var memuList :[[Model]] = [[Model]]()
......
func numberOfSections(in tableView: UITableView) -> Int {
return self.memuList.count;
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let groupMenus = self.memuList[section];
return groupMenus.count
}
......
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: kCellID, for: indexPath) as! MyCell
cell.accessoryType = .disclosureIndicator
cell.selectionStyle = .none
let groupMenus = self.memuList[indexPath.section];
if 0 == indexPath.row {
cell.cellPosition = .top
} else if indexPath.row == (groupMenus.count - 1) {
cell.cellPosition = .bottom
} else {
cell.cellPosition = .center
}
let model = groupMenus[indexPath.row]
......
return cell
}
以上