【问题标题】:UITableView keeps autoscrolling to topUITableView 不断自动滚动到顶部
【发布时间】:2015-10-04 21:14:31
【问题描述】:

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

【问题讨论】:

  • 显示更多代码。 viewDidLoad 外面一定有什么错误。
  • 您是否在该 viewController 中的任何位置设置 contentOffset 或 contentInsets?
  • @kostek - 谢谢 - 刚刚提供了 .swift 文件中的所有内容

标签: ios swift uitableview


【解决方案1】:

移动你的代码,除了

tableView.delegate = self
tableView.dataSource = self

查看WillAppear

【讨论】:

  • 假设我做对了(见下文) - 那没用:override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self } override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) 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 = CGRectMake(0,0,screenWidth,screenHeight - 40) 足以在没有任何额外约束的情况下放置视图
猜你喜欢
  • 2013-03-24
  • 2010-10-18
  • 2018-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-04
  • 1970-01-01
相关资源
最近更新 更多