![](/img/trans.png)
[英]how to scroll uitableview, and section header in the top, visible and scroll to
[英]Swift 2.0 UITableView Section Header scroll to top when tapped
所以我到處檢查,似乎無法找到我正在尋找的答案。 這是我的問題:我有一個UITableView,里面有不同的部分。 每個部分都有一個標題,當點擊時,它會擴展該部分並顯示它的行或單元格。 但是,當你點擊標題時,它會向下擴展它的部分,但它會保留在它的位置。 我希望該部分標題在單擊時移動到頂部。 下面是示例代碼。 我希望我能很好地解釋這一點。
這是節標題本身:
func configureHeader(view: UIView, section: Int) {
view.backgroundColor = UIColor.whiteColor()
view.tag = section
let headerString = UILabel(frame: CGRect(x: 40, y: 15, width: tableView.frame.size.width-10, height: 40)) as UILabel
headerString.text = sectionTitleArray.objectAtIndex(section) as? String
headerString.textAlignment = .Left
headerString.font = UIFont.systemFontOfSize(24.0)
view .addSubview(headerString)
let frame = CGRectMake(5, view.frame.size.height/2, 20, 20)
let headerPicView = UIImageView(frame: frame)
headerPicView.image = headerPic
view.addSubview(headerPicView)
let headerTapped = UITapGestureRecognizer (target: self, action:"sectionHeaderTapped:")
view .addGestureRecognizer(headerTapped)
}
這是sectionHeaderTapped函數:
func sectionHeaderTapped(recognizer: UITapGestureRecognizer) {
print("Tapping working")
print(recognizer.view?.tag)
let indexPath : NSIndexPath = NSIndexPath(forRow: 0, inSection:(recognizer.view?.tag as Int!)!)
if (indexPath.row == 0) {
var collapsed = arrayForBool .objectAtIndex(indexPath.section).boolValue
collapsed = !collapsed;
arrayForBool .replaceObjectAtIndex(indexPath.section, withObject: collapsed)
//reload specific section animated
let range = NSMakeRange(indexPath.section, 1)
let sectionToReload = NSIndexSet(indexesInRange: range)
self.tableView .reloadSections(sectionToReload, withRowAnimation:UITableViewRowAnimation.Fade)
}
}
謝謝!!
您可以在表視圖上使用scrollToRowAtIndexPath(_:atScrollPosition:animated :)將所述部分的第一行滾動到頂部位置。
一個好方法是使用值為行“NSNotFoud”的IndexPath,轉到部分的頂部😁
let indexPath = IndexPath(row: NSNotFound, section: 0)
self.scrollToRow(at: indexPath, at: .top, animated: true)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.