IOS双击UITableView中的单元格原因项不会滚动

我有一个UITableView在每个单元格中包含UITextField
当我单击单元格中的UITextField时,键盘将显示并覆盖我的单元格。 因此,我通过使用移动我的单元格到顶部。

 - (void)keyboardWasShown:(NSNotification*)aNotification { CGPoint scrollPoint = CGPointMake(0, self.activeInputView.frame.origin.y); [self.tableView setContentOffset:scrollPoint animated:YES]; } 

如果我单击每个单元格,我的应用程序工作正常。
然而,我用双击每个单元格(这意味着我很快点击它2次),我的单元格将停止滚动到顶部。

在这一行

CGPoint scrollPoint = CGPointMake(0, self.activeInputView.frame.origin.y);

可以是2个错误。

首先,你必须确保你已经捕获的inputView是你想要的视图。 其次,您应该使用适当的方法将提取的点转换为正确的视图。

请参阅参考资料: https : //developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/doc/uid/TP40006816-CH3-SW52

进一步的调查需要更多的背景。

TPKeyboardAvoiding似乎是一个解决scheme。

你确定scrollPoint是正确的吗?

我刚刚创build了一个具有固定偏移量的示例项目,一切正常。 在单击和双击某个UITableViewCellUITextField后,表格视图向上滚动。

 - (void)keyboardDidShow:(NSNotification *)notification { CGPoint point = CGPointMake(0.0, 30.0); [self.tableView setContentOffset:point animated:YES]; } 

而不是做手动调整你的tableview框架的代码,让我给你一个build议,你可以使用IQ键盘pipe理器的pod在cocoapods中可用,它将pipe理点击单元格的文本字段时,tableview将自动滚动。

尝试使用第三方库IQKeyboardManager它会帮助你解决这个问题。

https://github.com/hackiftekhar/IQKeyboardManager

在你的AppDelegate.m文件中replace下面的代码,然后你不需要使用scrollview方法来处理键盘:

 @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[IQKeyboardManager sharedManager] setEnable:YES]; [self.window makeKeyAndVisible]; return YES; } 

你有没有创build单独的UITableViewCell类?

我相信,当你滚动你的UITableView你的用户界面冻结,或者当你频繁地select两次你的UITableView停止响应或花时间来回应。

这是一个工作的例子。 您可以使用滚动视图的contentInsetscrollIndicatorInsets属性来避免出现键盘。 你应该注册键盘通知。 使用通知中的信息来确定键盘的大小 – 你永远不会知道这将是什么。

使用内容embedded是正确的方式来处理滚动视图。 如果您随后需要将编辑行滚动到视图中,请使用UITableView.scrollToRowAtIndexPath(_:,atScrollPosition:,animated 🙂

请注意,当用户隐藏/显示完成栏时,这是正确的。

 import UIKit import CoreGraphics // our Cell class class Cell : UITableViewCell { // cell reuse identifier for table view static let Identifier = "Cell" // the object the cell represents/displays. Could be anything you like var value:AnyObject? { didSet { // update our text field when our cell value is set. self.textField.text = value as? String } } // use a text field to display our contents, since that allows editing and showing the keyboard lazy var textField:UITextField = { let textField = UITextField() self.contentView.addSubview( textField ) return textField }() override func layoutSubviews() { super.layoutSubviews() self.textField.frame = contentView.bounds.insetBy(dx: 20, dy: 4 ) } } // table view data source class class DataSource : NSObject, UITableViewDataSource { var numberOfRows:Int { return items.count } let items = [ "Seoul", "São Paulo", "Bombay", "Jakarta", "Karachi", "Moskva", "Istanbul", "Mexico", "Shanghai", "Tokyo", "New" York, "Bangkok", "Beijing", "Delhi", "London", "Hong Kong", "Cairo", "Tehran", "Bogota", "Bandung", "Tianjin", "Lima", "Rio de Janeiro", "Lahore", "Bogor", "Santiago", "St Petersburg", "Shenyang", "Calcutta", "Wuhan" ] func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return numberOfRows } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier( Cell.Identifier ) as? Cell ?? Cell() cell.value = items[ indexPath.row ] return cell } } class ViewController : UIViewController { override func viewDidLoad() { super.viewDidLoad() // register for notifications when the keyboard appears: NSNotificationCenter.defaultCenter().addObserver( self, selector: "keyboardWillShow:", name: UIKeyboardWillChangeFrameNotification, object: nil) } override func viewDidLayoutSubviews() { tableView.frame = view.bounds } lazy var tableView:UITableView = { let tableView = UITableView() self.view.addSubview( tableView ) tableView.dataSource = self.dataSource tableView.delegate = self return tableView }() lazy var dataSource : DataSource = DataSource() // Handle keyboard frame changes here. // Use the CGRect stored in the notification to determine what part of the screen the keyboard will cover. // Adjust our table view's contentInset and scrollIndicatorInsets properties so that the table view content avoids the part of the screen covered by the keyboard @objc func keyboardWillShow( note:NSNotification ) { // read the CGRect from the notification (if any) if let newFrame = (note.userInfo?[ UIKeyboardFrameEndUserInfoKey ] as? NSValue)?.CGRectValue() { let insets = UIEdgeInsetsMake( 0, 0, newFrame.height, 0 ) tableView.contentInset = insets tableView.scrollIndicatorInsets = insets } } } // need to conform to UITableViewDelegate protocol since we are the table view's delegate extension ViewController : UITableViewDelegate { } // App set up stuff here: @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { lazy var window:UIWindow? = UIWindow() func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { window!.rootViewController = ViewController() window!.makeKeyAndVisible() return true } }