Advertisement
srk72

table rows corner radius

Apr 11th, 2023
989
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.12 KB | None | 0 0
  1.  
  2. extension SelectPreferencesVC: UITableViewDelegate, UITableViewDataSource {
  3.     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  4.        
  5.         let sectionItem = sections[section]
  6.        
  7.         if sectionItem.isOpen {
  8.             return sections[section].options.count + 1
  9.         } else {
  10.             return 1
  11.         }
  12.     }
  13.    
  14.     func numberOfSections(in tableView: UITableView) -> Int {
  15.         sections.count
  16.     }
  17.  
  18.     /*
  19.     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  20.         <#code#>
  21.     }*/
  22.    
  23.     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  24.        
  25.         if indexPath.row == 0 {
  26.            
  27.             let sectionData = sections[indexPath.section]
  28.            
  29.             guard let cell = tableView.dequeueReusableCell(withIdentifier: "preferenceSectionCell", for: indexPath) as? preferenceSectionCell else { return UITableViewCell() }
  30.             cell.title.text = sectionData.name
  31.            
  32.             return cell
  33.         } else {
  34.             let rowData = sections[indexPath.section].options[indexPath.row - 1]
  35.            
  36.             let sectionData = sections[indexPath.section]
  37.            
  38.             debugPrint("Count : \(sectionData.options.count), last index \(indexPath.row)")
  39.            
  40.             guard let cell = tableView.dequeueReusableCell(withIdentifier: "preferenceRowCell", for: indexPath) as? preferenceRowCell else { return UITableViewCell() }
  41.             cell.title.text = rowData
  42.             cell.bottomLine.isHidden = sectionData.options.count == indexPath.row ? true : false
  43.            
  44.             return cell
  45.         }
  46.     }
  47.    
  48.     func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  49.        
  50.         let radius: CGFloat = 17
  51.        
  52.         if tableView == preferenceTable {
  53.            
  54.             let sectionData = sections[indexPath.section]
  55.            
  56.             guard let cellNew = cell as? preferenceRowCell else { return }
  57.            
  58.             //Top Left Right Corners
  59.             let maskPathTop = UIBezierPath(roundedRect: cellNew.bgView.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: radius, height: radius))
  60.             let shapeLayerTop = CAShapeLayer()
  61.             shapeLayerTop.frame = cellNew.bgView.bounds
  62.             shapeLayerTop.path = maskPathTop.cgPath
  63.  
  64.             //Bottom Left Right Corners
  65.             let maskPathBottom = UIBezierPath(roundedRect: cellNew.bgView.bounds, byRoundingCorners: [.bottomLeft, .bottomRight], cornerRadii: CGSize(width: radius, height: radius))
  66.            
  67.             let shapeLayerBottom = CAShapeLayer()
  68.             shapeLayerBottom.frame = cellNew.bgView.bounds
  69.             shapeLayerBottom.path = maskPathBottom.cgPath
  70.  
  71.             //All Corners
  72.             let maskPathAll = UIBezierPath(roundedRect: cellNew.bgView.bounds, byRoundingCorners: [.topLeft, .topRight, .bottomRight, .bottomLeft], cornerRadii: CGSize(width: radius, height: radius))
  73.             let shapeLayerAll = CAShapeLayer()
  74.             shapeLayerAll.frame = cellNew.bgView.bounds
  75.             shapeLayerAll.path = maskPathAll.cgPath
  76.  
  77.             if (indexPath.row == 1)
  78.             {
  79.                 //cellNew.bgView.layer.mask = shapeLayerTop
  80.                 cellNew.bgView.applyCornerRadius(17, maskedCorners: [.layerMaxXMinYCorner, .layerMinXMinYCorner])
  81.             }
  82.          else if sectionData.options.count == indexPath.row
  83.             {
  84.                 cellNew.bgView.applyCornerRadius(17, maskedCorners: [.layerMinXMaxYCorner, .layerMaxXMaxYCorner])
  85.                 //cellNew.bgView.layer.mask = shapeLayerBottom
  86.             }
  87.         }
  88.     }
  89.    
  90.     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  91.         sections[indexPath.section].isOpen = !sections[indexPath.section].isOpen
  92.         tableView.reloadSections([indexPath.section], with: .fade)
  93.     }
  94. }
  95.  
  96.  
  97. struct Section {
  98.     let name: String
  99.     let options: [String]
  100.     var isOpen = false
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement