ios - UITableView 一直自动滚动到顶部

标签 ios swift uitableview

当用户尝试向下滚动表格(超过键盘高度)时,它会自动滚动回到表格顶部,使用户无法按下表格底部行中的任何键。如何禁用此自动滚动到顶部?请注意,这与 scrollsToTop 属性不同。

import UIKit

class KeyboardViewController: UIInputViewController, UITableViewDelegate, UITableViewDataSource {

var tableView: UITableView = UITableView()
let screenSize: CGRect = UIScreen.mainScreen().bounds


let buttonTitles = [
    "XX",
    "YY"
]

override func viewDidLoad() {
    super.viewDidLoad()

    let screenWidth = screenSize.width
    let screenHeight = screenSize.height
    tableView.frame = CGRectMake(0,0,screenWidth,screenHeight - 40)
    tableView.delegate = self
    tableView.dataSource = self

    tableView.registerClass(UITableViewCell.self,forCellReuseIdentifier: "cell")

    self.view.addSubview(tableView)


}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
    cell.textLabel?.text = self.buttonTitles[indexPath.row]
    cell.textLabel?.textAlignment = .Center
    cell.textLabel?.font = UIFont(name: "Helvetica Neue", size: 14)
    cell.textLabel?.textColor = UIColor.whiteColor()
    cell.textLabel?.numberOfLines = 0
    cell.backgroundColor = UIColor(red: 176/255, green: 15/255, blue: 15/255, alpha: 1.0)
    cell.preservesSuperviewLayoutMargins = false
    cell.layoutMargins = UIEdgeInsetsZero
    cell.separatorInset = UIEdgeInsetsZero
    return cell

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var string = buttonTitles[indexPath.row]
    (textDocumentProxy as! UIKeyInput).insertText("\(string)")
}

最佳答案

移动你的代码除了

tableView.delegate = self
tableView.dataSource = self

查看WillAppear

关于ios - UITableView 一直自动滚动到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31437032/

相关文章:

ios - Swift HTTP Post 请求返回网站的 HTML,而不是 JSON 响应

ios - 通过主视图 Controller 与 UICollectionViewCell 中的按钮交互

iphone - UITableView - 滚动到下一页后,多个部分会重复(使用 Parse)

ios - 在 Swift 中更改 iOS 应用程序的 TableView 和 TableView 单元格的位置和布局

ios - 错误域=NSCocoaErrorDomain 代码=512 "The operation couldn’ t be completed.The operation couldn't be completed.是一个目录

ios - UIView无限360度旋转动画?

ios - Swift 2 官方指南/文档链接?

ios - 如果用户从 UIAlertView 选择取消按钮,则停止从 UITable 中删除用户

c# - 在二维坐标系中查找分隔线

ios - 调整 UITableView 以显示隐藏 UIKeyboard 的最简单方法