ios - Swift 2.0 UITableView 部分标题在点击时滚动到顶部

标签 ios swift uitableview

所以我到处检查,似乎找不到我要找的答案。这是我的问题:我有一个包含不同部分的 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/

相关文章:

ios - 在数组中查找文本并返回索引号

swift - 为什么 BarButton 会转到 View 底部?

ios - 如何使用 Core Data 更新数据?

ios - 带有 UIRefreshControl 的 TableView 在重新加载时似乎有点故障?

ios - 主详细信息应用程序indexPathForSelectedRow问题

ios - 为什么在cellForItemAt中设置单元格属性不好?

ios - Swift 减少/展平字典<String,Dictionary> 到字典

ios - Xcode 无法启动应用程序。进程启动失败 : 4294967295

ios - 如何检查用户是否已经使用 OneDrive SDK for iOS 登录?

uitableview - MvvMCross UITableView 将本地属性绑定(bind)到 UITableViewCellAccessory.CheckMark