2012-03-09 97 views
43

我在我的UITableView中使用自定義UITableViewCell。這些UITableViewCell中的每一個都非常高,並在頂部包含UITextField在UITableViewCell中編輯UITextField時禁用UITableView的自動滾動

當用戶點擊UITextField進行編輯時,會出現一個鍵盤,並且UITableView會自動滾動,以便該單元位於屏幕的頂部。

問題是,這會將UITableView滾動到UITableViewCell的底部,而不是頂部。當UITableViewCell高並編輯時,UITextField位於頂部,因此您看不到UITextField。我知道如何以編程方式滾動UITableView,但我只是不知道如何禁用此自動滾動,以便我自己滾動UITableView。我怎樣才能做到這一點?

+0

你明白這一點嗎?遇到同樣的問題。 – 2012-03-23 18:40:17

+0

@SteveBaughman你找到解決方案嗎? – HighFlyer 2012-11-07 10:36:25

+0

nope :(我實際上停止使用表視圖,因爲這個和其他一些原因...:/ – 2012-11-07 15:23:17

回答

59

自動滾動行爲位於UITableViewController功能。

要禁用自動滾動,我發現有兩種方式:

  1. 使用,而不是簡單地UITableViewController一個UIViewController - 設定你自己的數據源和委託。
  2. 重寫viewWillAppear方法,不叫[super viewWillAppear: animated]

使用這兩種解決方案,您禁用不僅自動滾動,但也有一些其他的不錯,但不是必需的功能,在Apple's類的概要描述參考:

https://developer.apple.com/documentation/uikit/uitableviewcontroller

+3

我有同樣的問題和解決方案2工作。我已經在iOS 5.0.0及以上版本中測試過,目前爲止(當前版本爲6.1.2)。不支持4,但嘿,這是2013年,所以我們可以說5.0是目前支持的最低版本。 – Cocoadelica 2013-03-01 09:53:28

+0

問題在於插入內容不適合容納鍵盤,至少在iOS 7.0中不適用。 6.這意味着需要更多代碼來手動執行此操作:捕捉鍵盤通知,......難看 – 2014-03-05 20:41:49

+0

選項2與我合作,謝謝 – 2014-04-27 23:15:53

-2

您是否嘗試將「scrollsToTop」 - tableview屬性設置爲NO。默認情況下是YES。

+0

嘗試過它,但它沒有工作... scrollToTop doesn'不管怎麼處理...謝謝你的回答! – 2012-03-10 18:53:18

-4

你可以嘗試做如下:

self.tableView.scrollEnabled = NO; 

這應該禁用tableview中的滾動視圖。

+1

這並不妨礙自動滾動UITableViewController時,在UITableViewCell中的文本字段上輕擊時會做什麼。 – Gujamin 2012-11-28 19:46:30

3

你可以做到以下幾點:

- (void)registerForKeyboardNotifications 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillShow:) 
               name:UIKeyboardWillShowNotification object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardDidShow:) 
               name:UIKeyboardDidShowNotification object:nil]; 
} 

- (void)unregisterForKeyboardNotifications 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil]; 
} 

- (void)keyboardWillShow:(NSNotification *)notification 
{ 
    self.tableView.scrollEnabled = NO; 
} 

- (void)keyboardDidShow:(NSNotification *)notification 
{ 
    double delayInSeconds = 0.3; 
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); 
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
      self.tableView.scrollEnabled = YES; 
    }); 
} 

然後實現這個UIScrollViewDelegate方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    if (! self.tableView.scrollEnabled) 
     [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO]; 
} 

!但要注意的是,如果用戶在UITextField中的位置點擊鍵盤覆蓋的位置,它將不會滾動。

從我的角度來看,最好的辦法是確保所有包含UITextField的頂部到另一個的單元格在鍵盤顯示時將可見。

+0

You da man art。謝謝,工作。 – 2014-11-19 00:08:20

+0

-scrollViewDidScroll:確實在自動滾動發生時調用,這裏是最好的解決方案 – 2015-07-15 11:32:04

6

定義屬性您UITableViewController

@property (nonatomic) BOOL scrollDisabled; 
@property (nonatomic) CGFloat lastContentOffsetY; 

在打電話前becomeFirstResponder

// Save the table view's y content offset 
lastContentOffsetY = tableViewController.tableView.contentOffset.y; 
// Enable scrollDisabled 
scrollDisabled = YES; 

下面的代碼添加到您的表視圖控制器:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    if (self.scrollDisabled) { 
     [self.tableView setContentOffset:CGPointMake(0, lastContentOffsetY)]; 
    } 
    ... 
} 

在調用resignFirstResponder,s et scrollDisabled = NO

+0

這不是scrollview問題,是tableViewController類。正確的答案是覆蓋viewWilAppear – 2017-08-02 12:49:21

1

這個問題對我來說並不是很大,它滾動,但它是從屏幕上編輯文本視圖。

因此,而不是防止滾動,我只是rescroll的實現代碼如下的地方,我希望在編輯被觸發,這樣的:

public func textViewShouldBeginEditing(textView: UITextView) -> Bool {    
    dispatch_async(dispatch_get_main_queue()) { 
    tableViewController.tableView.scrollToRowAtIndexPath(self.indexPath!, atScrollPosition: UITableViewScrollPosition.Middle, animated: true) 
    } 
    return true 
} 
1

不幸的是,覆蓋-viewWillAppear:不爲我在工作的iOS 8.

這裏是我的解決方案(如在實施的UITableViewController):

- (void)viewDidLoad { 

[super viewDidLoad]; 

[[NSNotificationCenter defaultCenter] removeObserver:self.tableView name:UIKeyboardWillShowNotification object:nil]; 

[[NSNotificationCenter defaultCenter] removeObserver:self.tableView name:UIKeyboardWillHideNotification object:nil]; 
} 

由於自動滾動行爲由UIKeyboard秀調用/隱藏通知,所以不要觀察它們。

+0

這仍然適合你?無法在iOS 11/Xcode 9中使用它 – simonthumper 2017-10-30 11:29:12

1

最好的方法是繼承UITableView,然後重寫setContentOffset(_ contentOffset: CGPoint, animated: Bool)而不是調用super.setContentOffset(_ contentOffset: CGPoint, animated: Bool)。在這種方法中,視圖控制器正在執行自動滾動。

override func setContentOffset(_ contentOffset: CGPoint, animated: Bool) 
{ 
    //Do nothing 
} 
相關問題