2016-09-25 74 views
3

我有一個UITableViewController的子類,它顯示了幾行(確切地說是8行),其中第一行是一個包含UITextField的自定義單元格。加載視圖時,我立即通過調用becomeFirstResponder對該textfield鍵盤彈出。這一切工作正常,直到我從iOS 9升級到iOS 10。現在視圖跳轉到視圖的底部,鍵盤覆蓋了我的最後兩行,而我的TextField不在可見區域。當我註釋掉becomeFirstResponder呼叫時,跳躍消失,但當然我失去了鍵盤彈出。這是我的代碼看起來像......iOS 10 - UITableViewController在加載時滾動到底部

class UserProfileTableViewController: UITableViewController { 

    private var userInfoCell: UserInfoTableViewCell? 
    ... 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    if indexPath.section == 0 { 
    userInfoCell = tableView.dequeueReusableCellWithIdentifier("userInfoCell", forIndexPath: indexPath) as? UserInfoTableViewCell 

    if !editMode { 
     userInfoCell?.nameField.becomeFirstResponder() 
    } 

    return userInfoCell! 
    } 
    else if indexPath.section == 1 { 
    .... 

然後我自定義表格視圖單元格看起來像這樣...

class UserInfoTableViewCell: UITableViewCell, UITextFieldDelegate,, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

@IBOutlet weak var nameField: UITextField! 
@IBOutlet weak var photoImageView: UIImageView! 
... 

override func awakeFromNib() { 
    super.awakeFromNib() 
    // listen for when user touches a textfield 
    nameField.delegate = self 
    ... 
} 

override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 
} 

... 

事情我已經嘗試:

  1. 調用userInfoCell?.nameField.becomeFirstResponder()viewWillAppear()
  2. 調用userInfoCell?.nameField.becomeFirstResponder() in viewDidAppear()
  3. 致電nameField.becomeFirstResponder()UserInfoTableViewCell.awakeFromNib()
  4. 撥打userInfoCell?.nameField.becomeFirstResponder()後致電tableView.scrollToRowAtIndexPath()
  5. 去除super.viewWillAppear()在我的這些viewWillAppear()

都沒有爲我工作。

+0

試試這個:'讓細胞= tableView.dequeueReusa .....'/'回報cell' –

+0

感謝您的建議,但沒有不幸的是,這樣做。我得到相同的滾動到底部問題。 – Coder1224

+0

嗯,我是否堆棧堆棧溢出? – Coder1224

回答

0

推遲becomeFirstResponder()解決了這個問題對我來說

dispatch_async(dispatch_get_main_queue(), { 
    userInfoCell?.nameField.becomeFirstResponder() 
}) 
+0

這使得它在屏幕上停留在頂部!但是,現在當屏幕向桌面滑動時,鍵盤會出現奇怪的閃爍,然後消失,然後又出現另一個鍵盤。也許是因爲它在一個tableview委託中調用,所以被稱爲多次? 我見過這個'dispatch_async()'作爲許多SO帖子的解決方案,但我毫不猶豫地創建線程作爲一般規則。那麼所有的方法都是這樣的 - 創建一個線程來等待主線程準備好了嗎? – Coder1224