Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableView保持自动滚动到顶部_Ios_Swift_Uitableview - Fatal编程技术网

Ios UITableView保持自动滚动到顶部

Ios UITableView保持自动滚动到顶部,ios,swift,uitableview,Ios,Swift,Uitableview,当用户尝试向下滚动表格(超过键盘高度)时,它会自动滚动回表格顶部,用户无法按下表格底部行中的任何键。如何禁用此自动滚动到顶部?注意,这与scrollsToTop属性不同 import UIKit class KeyboardViewController: UIInputViewController, UITableViewDelegate, UITableViewDataSource { var tableView: UITableView = UITableView() let scree

当用户尝试向下滚动表格(超过键盘高度)时,它会自动滚动回表格顶部,用户无法按下表格底部行中的任何键。如何禁用此自动滚动到顶部?注意,这与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

要查看,将出现

显示更多代码。viewDidLoad之外一定有什么问题。您是否在该viewController中的任何位置设置contentOffset或contentInsets?@kostek-谢谢-刚刚提供了.swift文件中的所有内容假设我做得对(见下文)-这不起作用:
override func viewDidLoad(){super.viewDidLoad()tableView.delegate=self tableView.dataSource=self}override func viewwillappease(动画:Bool){super.viewwillappease(动画)let screenWidth=screenSize.width let screenHeight=screenSize.height tableView.frame=CGRectMake(0,0,screenWidth,screenHeight-40)tableView.registerClass(UITableViewCell.self,forCellReuseIdentifier:“cell”)self.view.addSubview(tableView)}
您是否为表视图添加了任何约束?对不起,我是新手,但假设tableView.frame=cDirectMake(0,0,屏幕宽度,屏幕高度-40)是否有足够的信息来放置视图而无需任何其他约束