所以我到处检查,似乎找不到我要找的答案。这是我的问题:我有一个包含不同部分的 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:) 在表格 View 上将所述部分的第一行滚动到顶部位置。
关于ios - Swift 2.0 UITableView 部分标题在点击时滚动到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34771711/