为分组表视图中的每个部分添加阴影
如图所示,我想在表视图部分添加阴影。 就像表视图有4个部分一样,表视图中会有4个阴影视图。
func numberOfSections(在tableView:UITableView中) – > Int {return 3}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 3 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { switch processIndex { return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 44 } func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { return UIView } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 40 }
我尝试过自定义[String : UIView]
。 这将适用于最少的行数。 如果您的单元设计是静态的,这将满足您的设计。 如果要访问UITableViewCell
某些属性,则必须使用子视图。
- 多个部分只有一行。 相应行中只有一个视图(
lookView
)。 - 我们必须根据需求以编程方式创建n个
UIView
。 -
UITapGesture
到TableView
。TableView
格式为Grouped
,Separator
为.none
。
以下代码将给出如下输出:
//Global Variable: @IBOutlet weak var tblVw: UITableView! var rowItemsDict = [String : [String]]() var rowHeight = [String : CGFloat]() var eachRowSubViews = [String : UIView]() override func viewDidAppear(_ animated: Bool) { rowItemsDict = ["0" : ["welcome", "hello", "wow", "Good", "Bad"], "1" : ["Asia", "Europe", "America"], "2" : ["Mcdonald", "Pizza", "Fries"]] for i in 0.. Int { return rowItemsDict.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SampleTableViewCell print("\n\ncellFor") cell.backgroundColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 0.0) cell.separatorVw.backgroundColor = UIColor(red: 250/255, green: 250/255, blue: 250/255, alpha: 0.0) tableView.clipsToBounds = true let gettingVw : UIView = eachRowSubViews[String(indexPath.section)]! cell.lookVw.addSubview(gettingVw) cell.lookVw.layer.cornerRadius = 10 cell.separatorVw.backgroundColor = UIColor.clear cell.selectionStyle = .none return cell } func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { let cell = cell as! SampleTableViewCell cell.lookVw.dropOnlyOneShadowLeft(getSize: CGSize.zero) cell.lookVw.backgroundColor = UIColor.white } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return (rowHeight[String(indexPath.section)]! ) } //ROW HEIGHT CALCULATION func getShadowViewHeight(defaultHeight: CGFloat, numberOfItems: Int) -> CGFloat { let requiredHeight = CGFloat(Int(defaultHeight) * numberOfItems) return requiredHeight } //TAP GESTURE ACTION @objc func tapEachView(sender: UITapGestureRecognizer) { let touchPoint = sender.location(in: tblVw) let index = tblVw.indexPathForRow(at: touchPoint) if (index == nil) { print("long press on table view but not on a row"); } else { let cell = tblVw.cellForRow(at: index!) as! SampleTableViewCell let pointInCell = cell.convert(touchPoint, from: tblVw) let innerSubVw = cell.lookVw.subviews print("pointInCellpointInCell) ", pointInCell) for sVw in innerSubVw[0].subviews { let pointY = pointInCell.y - 5 let comparePoint = sVw.frame.origin.y if pointY >= comparePoint && pointY <= comparePoint + 39 { print("sVwsVwsVwsVwsVw ", sVw.tag) //DO YOUR ACTION IN RESPECTIVE VIEW CLICK return } } } } extension UIView { func dropOnlyOneShadowLeft(getSize: CGSize) { self.layer.shadowColor = UIColor.lightGray.cgColor self.layer.shadowOpacity = 0.6 self.layer.shadowOffset = getSize self.layer.shadowRadius = 3 } }
- 将文本分配给UILabel(iOS,Swift 4,XCode9)时内存泄漏
- 如何在tableview中相互交换两个自定义单元格?
- 通过UIActivityViewController(Twitter,Facebook,Instagram)与hashtag共享图像
- 在Chrome上模拟iPhone X.
- 在Swift中更新的UILabel文本拒绝显示
- iPad键盘不会从使用模型视图控制器样式的视图控制器调用的Facebook对话框中消除UIModalPresentationFormSheet
- 如何使用StoryBoard在UITabBarController中设置选定的选项卡?
- 在添加标签栏控制器之后,放开不触发的segue和导航button项目
- 使用Django rest框架开发iOS客户端应用