2012-02-28 23 views
3

目前我有一個UITableView與自定義單元格,並在每個單元格中有一個UITextField,問題是有時UITextField由UIKeyboard覆蓋。將自定義UITableViewCell滾動到鍵盤右上方?

所以現在我有了UIKeyboard的Y座標,並且我的UITableView在單元格中正常工作。

所以我幾乎可以使用Y座標(浮點數),以便將我的UITableView滾動到該Y座標加上單元格的高度,以便在UIKeyboard上方正確顯示它?

另外,當我的鍵盤隱藏,我將如何重置UITableView到它的正常位置?

任何意見將不勝感激!

謝謝!

CustomCell *cell = (CustomCell*)[[thetextField superview] superview]; 
    CGRect cellRect = [cell convertRect:cell.frame toView:self.view]; 
    float bottomCell = cellRect.origin.y - cellRect.size.height; 
    if (bottomCell >= keyboardY) { 
     NSIndexPath *indexPath = [thetableView indexPathForCell:cell]; 
     [thetableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
    } 
+0

'scrollToRowAtIndexPath'方法知道你的tableView的可見部分,所以你不需要if語句。 – inorganik 2012-09-17 19:36:05

回答

2

一方或另一方UITableView

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated; 
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated; 

我猜,控制不進入if語句的。考慮跳過y座標檢查而只是滾動表視圖而不管它在哪裏。

CustomCell *cell = (CustomCell *)[[theTextField superview] superview]; 
NSIndexPath *indexPath = [theTableView indexPathForCell:cell]; 
[theTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 

OK,如果你確定這是越來越調用,然後確保你的indexPath不爲零,如果電池不在的tableView或者如果它不是一個細胞可能發生。

+0

我試了第一個,我有索引路徑,我做UITableViewScrollPositionTop,它不滾動任何地方 – 2012-02-28 02:54:55

+0

請發佈您的代碼。 – QED 2012-02-28 03:00:06

+0

我發佈了上面:) P.S - 如果語句被調用(用NSLogs確認) – 2012-02-28 03:03:19

0

只要顯示鍵盤,UITableView就會自行調整。

但是,如果您未將自定義單元格的UITableViewCellSelectionStyle設置爲UITableViewCellSelectionStyleNone,則不會這樣做。

我通過使用下面的代碼解決。

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];