2015-07-10 14 views
0

我的tableView大小由自動版式(底對底的佈局指南,頂到另一個視圖等,但首先的UISearchBar頂部佈局指南)中設置: My tableView的iOS的TableView時示出爲具有鍵盤選擇細胞錯indexPath

控制器用的tableView:

Controller settings

我需要改變表中的偏移時,將顯示鍵盤,使我有兩種方法:

// MARK: - Keyboard 
func keyboardWasShown (notification: NSNotification) { 
    let info: NSDictionary = notification.userInfo! 
    let value: NSValue = info.valueForKey(UIKeyboardFrameBeginUserInfoKey) as! NSValue 
    let keyboardSize: CGSize = value.CGRectValue().size 

    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0) 
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset 

} 

func keyboardWillBeHidden (notification: NSNotification) { 
    self.tableView.contentInset = UIEdgeInsetsZero 
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero 
} 

它的工作,但我有問題時,鍵盤顯示。最後一個項目不能選擇,而不是我得到的前一個項目。我點擊了最後一個項目的位置,它應該導航到最後一個項目的詳細信息頁面,而是看到之前項目的詳細信息頁面。這不是所有項目的轉移,而是最後一個項目,當我過濾到只有一個項目它的工作正常。當鍵盤被隱藏(並且物品仍然被過濾)時,它也可以(它選擇正確的東西)。所以我想這個問題一定在這裏:

self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0) 
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset 

那麼哪裏可能會出現問題?感謝您的幫助

+0

我想你也應該更新頂部的高度,以適應搜索欄和分段控制。 – Dree

+0

不,我只是試過了,它沒有幫助,當我滾動到頂部時,我的空間太大了。 –

+0

您是否嘗試過從'viewDidLoad:'添加額外空間? – Dree

回答

3

我得到了我的解決方案。我正在使用UIKeyboardWillHideNotification,並且在didSelectRowAtIndexPath之前調用了方法keyboardWillBeHidden,所以contentInsettableView被設置回UIEdgeInsetsZero,然後出現了錯誤的indexPath。所以,現在我用keyboardDidHide代替keyboardWillBeHidden

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardDidShowNotification, object: nil) 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil) 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidHide:", name: UIKeyboardDidHideNotification, object: nil) 

... 

func keyboardDidHide (notification: NSNotification) {  
    self.tableView.contentInset = UIEdgeInsetsZero 
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero 
} 
1

因此,假設keyboardHeight是存儲您的鍵盤高度(注意,因爲鍵盤框架可以跨設備而異),試試這個:

CGRect *frame = [tableView frame]; 
frame.size.height -= keyboardHeight; 
[tableView setFrame:frame] 

做同樣的事情(但+=取代-=)當鍵盤隱藏時。

+0

由於某種原因,我的底部偏移量錯誤,這並沒有幫助。但感謝幫助,它幫助我找到解決方案。 –

相關問題