如何在上下滚动时显示/隐藏UITableView标题?

4

我已经将一个HeaderView(来自.xib文件)添加到了UITableView中,在加载ViewController时它会正常显示。但是,当我向上滚动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

}
}
2个回答

7

在Storyboard中->选中你的TableView->进入属性检查器->将类型从Plain改为Grouped
输入图像描述


我正在检查@Nishant Bhindi,稍后会告诉你。 - anuj
你能展示一下在给属性Grouped赋值后得到的输出吗? - Nishant Bhindi
仍然与之前显示的相同 @Nishant Bhindi - anuj
请参考以下链接:https://dev59.com/v1sV5IYBdhLWcg3w0xsK。该链接涉及Swift 2.0中如何使表格视图的标题栏跟随单元格移动。 - Nishant Bhindi

4

在 Swift 5.0 中

以编程方式实现此操作的方法是使用 UITableView.Style.grouped

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接