【问题标题】:How to Show/Hide UITableView Header when scrolling up & down?上下滚动时如何显示/隐藏 UITableView 标题?
【发布时间】:2019-10-01 00:37:52
【问题描述】:

我已将HeaderView(来自.xib)添加到UITableView,当我加载ViewController.But 时它会正确显示当我向上滚动到tableView 的任何位置时如何隐藏tableView 标题和当我向下滚动时显示 HeaderView。如果有人可以帮助我,将不胜感激。下面是我的代码:

class ViewController: UIViewController,UITableViewDataSource,UIScrollViewDelegate {


@IBOutlet var myTable : UITableView!
var array = ["See All Local Deals","Food & Drink","Kids Activities","Taxi" , "Shopping" , "Local Tradesmen" , "Tattoo shop" , "Car Hire" , "Health & Fitness" , "Beauty & Spas" , "Home & Garden" , "Local Services" , "Nightlife" , "Wedding Planning" , "Holiday rentals"]

lazy var headerView : HeaderView? = {
    return Bundle.main.loadNibNamed("HeaderView", owner: self, options: nil)?[0] as? HeaderView

}()

override func viewDidLoad() {
    super.viewDidLoad()

    self.myTable.dataSource = self
  self.myTable.tableHeaderView = headerView

}

func numberOfSections(in tableView: UITableView) -> Int {
 return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return array.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyCell

    cell.lblText.text = array[indexPath.row]
    return cell

}
}

【问题讨论】:

    标签: uitableview swift3 scrollview


    【解决方案1】:

    在故事板中-> 选择您的表格视图-> 使用属性检查器-> 并将类型从普通更改为分组

    【讨论】:

    • 我正在检查@Nishant Bhindi 并尽快通知您
    • 您能否显示分配该属性 Grouped 后得到的输出?
    • 仍然和之前显示的一样 @Nishant Bhindi
    【解决方案2】:

    在 Swift 5.0 中

    执行此操作的编程方式是使用UITableView.Style.grouped

    self.tableView = UITableView(frame: .zero, style: .grouped)
    

    【讨论】:

      猜你喜欢
      • 2016-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-26
      • 2023-03-08
      相关资源
      最近更新 更多