2012-10-04 29 views

回答

0

我在過去幾次做過這個,雖然不是很喜歡它。 我的解決方案是將表格向上移動,使選定的單元格保持可見。

首先,我增加了一些觀察家知道當鍵盤出現或消失

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil]; 

然後我做了這樣的事情,推臺達:

- (void)keyboardWillShowNotification:(NSNotification*)notification { 


    CGSize kbSize = [[notification.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
    double animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 
    CGPoint newCenter = self.myable.center; 

    NSLog(@"Height: %f Width: %f", kbSize.height, kbSize.width); 
    // Portrait: Height: 264.000000 Width: 768.000000 
    // Landscape: Height: 1024.000000 Width: 352.000000 

    if([[UIApplication sharedApplication] statusBarOrientation] < 3) { 
     newCenter = CGPointMake(newCenter.x, heightP - kbSize.height - self.myTable.frame.size.height/2); 
    } 
    else { 
     newCenter = CGPointMake(newCenter.x, heightL - kbSize.width - self.myTable.frame.size.height/2); 
    } 



    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:animationDuration]; 
    self.myTable.center = newCenter; 
    [UIView commitAnimations]; 
} 

- (void)keyboardWillHideNotification:(NSNotification*)notification { 
    NSLog(@"keyboard disappeared"); 
    double animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 
    CGPoint newCenter = self.myTable.center; 

    if([[UIApplication sharedApplication] statusBarOrientation] < 3) { 
     newCenter = CGPointMake(newCenter.x, 0 + self.lmyTable.frame.size.height/2); 
    } 
    else { 
     newCenter = CGPointMake(newCenter.x, 0 + self.myTable.frame.size.height/2); 
    } 



    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:animationDuration]; 
    self.myTable.center = newCenter; 
    [UIView commitAnimations]; 

} 

基本上,我讀了鍵盤並將其添加到桌子的中央,向上推,然後在鍵盤消失時移除相同的高度。

現在,有一個趕上! 我將它從我的代碼中刪除,因爲它非常具體,但只有當鍵盤覆蓋您的單元格時,您必須小心移動表格!否則,您最終會將可見單元格推到屏幕頂部。 這很大程度上取決於您的視圖設置,邊框,表格的大小等等,所以我無法幫助您完成這部分任務,但我希望您有基本的想法!

好運

+0

太棒了:)。謝謝! – user1704644

0

更簡單...在你的tableview的委託didSelectRowAtIndexPath使用scrollToRowAtIndexPath:<#(NSIndexPath *)#> atScrollPosition:<#(UITableViewScrollPosition)#> animated:<#(BOOL)#>滾動到你的細胞。你可能不得不改變你的tableview的框架,並在單元格被取消選擇時再改回它。或者我們滾動插入屬性滾動。

+0

如果單元碰巧位於桌子的底部,它將保持在那裏,並且將被鍵盤覆蓋。最後,您仍然必須檢測鍵盤將會出現/消失,並相應地調整框架。相信我,去過那裏,做到了。事實上,當我與這個 – BBog

+0

奮鬥的時候,我嘗試了第一件事,那就是我知道你的方法是有效的......實際上,如果我沒有錯,它的靈感來源於蘋果在他們的一個項目中的解決方案......可以不記得哪一個,但我確定我看到了類似的解決方案,雖然它不是在tableView中,但在scrollView中(我的觀點沒有太大差別) –

+0

我不知道蘋果的解決方案,它可能是。在嘗試在他們的UITableView API中找到類似的東西之後,我自己提出了這個問題,但我從來沒有想過這是一個好的解決方案,它似乎相當強制而且不夠優雅, – BBog

相關問題