gpt4 book ai didi

swift - titleForHeaderInSection 不要移动

转载 作者:行者123 更新时间:2023-11-30 13:27:10 25 4
gpt4 key购买 nike

我有一个 UITableviewController ,其标题 View 类似于:

https://github.com/andersonkxiass/UITableViewHeader

但是我的表格中需要有一个标题标题,在第一个位置它是正确的,但是当我开始移动表格标题时保持静态

这是我的代码:

class tableComent: UITableViewController {

var testComentName = [String]()


var headerView = UIView()

var viewHeight:CGFloat = 350 {
didSet {
moveHeader()
}
}

var scrollOffset:CGFloat = 0 {
didSet {
moveHeader()
}
}

private func loadNib() -> UIView {
let bundle = NSBundle(forClass: self.dynamicType)
return bundle.loadNibNamed("headerTable", owner: nil, options: nil)[0] as! UIView
}

func moveHeader() {
let viewHeight = (scrollOffset >= 0) ? self.viewHeight : self.viewHeight - scrollOffset
headerView.frame = CGRect(x: 0, y: -viewHeight, width: view.bounds.width, height: viewHeight)
}

var backView = UIView()

override func viewDidLoad() {
super.viewDidLoad()
headerView = loadNib()

backView.frame = CGRect(x: 0, y: 0 , width: view.bounds.width, height: view.bounds.height)
backView.backgroundColor = tableView.backgroundColor

view.addSubview(backView)
view.sendSubviewToBack(backView)

// Set the contentInset to make room for the image.
tableView.contentInset = UIEdgeInsets(top: viewHeight, left: 0, bottom: 0, right: 0)

// Add the imageView to the TableView and send it to the back.
view.addSubview(headerView)
view.sendSubviewToBack(headerView)


self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None

testComentName = ["Andres Gómez","Beatriz Gómez", "Francisco Manzo", "Rodolfo Martínez","Enrique Vega","Ana Lemus"]
loadTable()

}

override func viewWillAppear(animated: Bool) {
loadTable()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

override func viewDidLayoutSubviews() {
// Update the image position after layout changes.
moveHeader()
}

// Update scrollOffset on tableview scroll
override func scrollViewDidScroll(scrollView: UIScrollView) {
scrollOffset = tableView.contentOffset.y + viewHeight
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return testComentName.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CellComentYo", forIndexPath: indexPath) as! celdaComentYo
cell.selectionStyle = .None
cell.nomLabel.text = testComentName[indexPath.row]
return cell
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

return "My Title:"
}

func loadTable() {
self.tableView.reloadData()
}

}

我猜问题出在scrollViewDidScroll中,但我不知道如何启用滚动标题。

谢谢!

最佳答案

我一直都是这样做的:

  1. 在 Controller 顶部添加一个 imageView
  2. 添加带有空标题的 tableView(与图像大小相同

        //ImageView
    imageView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: self.headerHeight)
    imageView.image = UIImage(assetIdentifier: headerImageAsset)
    imageView.contentMode = UIViewContentMode.ScaleAspectFill
    imageView.clipsToBounds = true

    //Create empty header for the table
    let headerView = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: self.headerHeight))
    headerView.backgroundColor = UIColor.clearColor()
    tableView.tableHeaderView = headerView

在滚动委托(delegate)上:

func scrollViewDidScroll(scrollView: UIScrollView) {
let yScroll = scrollView.contentOffset.y

if yScroll < 0.0 {
imageView.frame = CGRectMake(yScroll, 0, screenWidth - (yScroll * 2), self.headerHeight - yScroll)
} else {
self.imageView.frame = CGRectMake(0, -yScroll/3, screenWidth, self.headerHeight)
}
}

关于swift - titleForHeaderInSection 不要移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37034771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com