2016-01-26 13 views
5

Ho un UITableView che contiene UITextField in ogni cella
quando clicco UITextField nella cella, la tastiera mostrerà e coprire il mio cellulare. Pertanto, sposto la mia cella verso l'alto usando.IOS doppio clic sulla voce causa cellula UITableView non scorrerà

- (void)keyboardWasShown:(NSNotification*)aNotification 
{ 
    CGPoint scrollPoint = CGPointMake(0, self.activeInputView.frame.origin.y); 
    [self.tableView setContentOffset:scrollPoint animated:YES]; 
} 

Se uso solo click su ogni cella, il mio lavoro di applicazione bene.
Tuttavia, io uso doppio clic su su ogni cella (significa che lo tocco due volte molto rapidamente), la mia cella si fermerà scorrere verso l'alto.

+0

fare doppio clic sul campo di testo nella cella u intendeva? – Tj3n

+0

significa che lo tocco 2 volte –

+0

Non puoi semplicemente disabilitare l'interazione dell'utente sul tuo campo di testo quando la tua vista tabella sta scorrendo? – Randy

risposta

1

piuttosto che fare il codice per regolare manualmente la cornice Tableview, mi permetta di darle un suggerimento, è possibile utilizzare IQKeyboardManager il cui baccello è disponibile in cocoapods e gestirà di quando si fa clic sul campo di testo del cellulare, Tableview scorrerà automaticamente.

+0

grazie per il tuo suggerimento ma voglio gestirlo. Perché mostrerò una tabella di visualizzazione automatica sotto il campo di testo quando clicco su di esso –

2

TPKeyboardAvoiding sembra essere una soluzione.

1

Prova ad utilizzare la libreria di terze parti IQKeyboardManager che ti aiuterà a risolvere questo problema.

https://github.com/hackiftekhar/IQKeyboardManager

Sostituire seguente codice nel file AppDelegate.m e quindi non c'è bisogno di utilizzare il metodo ScrollView per gestire tastiera: -

@implementation AppDelegate 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    [[IQKeyboardManager sharedManager] setEnable:YES]; 

    [self.window makeKeyAndVisible]; 
    return YES; 

} 
3

In questa linea

CGPoint scrollPoint = CGPointMake(0, self.activeInputView.frame.origin.y);

possono essere 2 errori.

Innanzitutto, è necessario assicurarsi che InputView che è stato acquisito sia la vista desiderata. In secondo luogo, è necessario convertire il punto recuperato per correggere la vista utilizzando il metodo appropriato.

Vai alla tabella: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/doc/uid/TP40006816-CH3-SW52

per ulteriori indagini hanno bisogno di più contesto.

2

Sei sicuro che lo scrollPoint sia corretto?

Ho appena creato un progetto di esempio con un offset fisso e tutto funziona perfettamente. La vista tabella scorre verso l'alto dopo singolo e doppio tocco su UITextField di un certo UITableViewCell.

- (void)keyboardDidShow:(NSNotification *)notification { 
    CGPoint point = CGPointMake(0.0, 30.0); 
    [self.tableView setContentOffset:point animated:YES]; 
} 
1

Hai creato una classe UITableViewCell separata?

Credo che mentre si scorre il tuo UITableView l'interfaccia utente si blocca o quando si spunta due volte di frequente il tuo UITableView smette di rispondere o di prendere tempo per rispondere.

0

Ecco un esempio funzionante. Si utilizza la visualizzazione di scorrimento contentInset e scrollIndicatorInsets proprietà per evitare la tastiera quando viene visualizzata. Devi registrarti per le notifiche della tastiera.Usa le informazioni nelle notifiche per determinare le dimensioni della tastiera - non sai mai quale sarà.

L'utilizzo di inset di contenuto è il modo corretto di gestirlo per le visualizzazioni di scorrimento. Se successivamente necessario scorrere la riga di modifica in vista, utilizzare UITableView.scrollToRowAtIndexPath (_ :, atScrollPosition :, animata :)

Si noti che la fa la cosa giusta quando l'utente si nasconde/mostra la barra completamenti.

import UIKit 
import CoreGraphics 

// our Cell class 
class Cell : UITableViewCell 
{ 
    // cell reuse identifier for table view 
    static let Identifier = "Cell" 

    // the object the cell represents/displays. Could be anything you like 
    var value:AnyObject? { 
     didSet { 
      // update our text field when our cell value is set. 
      self.textField.text = value as? String 
     } 
    } 

    // use a text field to display our contents, since that allows editing and showing the keyboard 
    lazy var textField:UITextField = { 
     let textField = UITextField() 
     self.contentView.addSubview(textField) 
     return textField 
    }() 

    override func layoutSubviews() { 
     super.layoutSubviews() 
     self.textField.frame = contentView.bounds.insetBy(dx: 20, dy: 4) 
    } 
} 

// table view data source class 
class DataSource : NSObject, UITableViewDataSource 
{ 
    var numberOfRows:Int { return items.count } 
    let items = [ "Seoul", "São Paulo", "Bombay", "Jakarta", "Karachi", "Moskva", "Istanbul", "Mexico", "Shanghai", "Tokyo", "New" York, "Bangkok", "Beijing", "Delhi", "London", "Hong Kong", "Cairo", "Tehran", "Bogota", "Bandung", "Tianjin", "Lima", "Rio de Janeiro", "Lahore", "Bogor", "Santiago", "St Petersburg", "Shenyang", "Calcutta", "Wuhan" ] 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     return numberOfRows 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let cell = tableView.dequeueReusableCellWithIdentifier(Cell.Identifier) as? Cell ?? Cell() 
     cell.value = items[ indexPath.row ] 
     return cell 
    } 
} 

class ViewController : UIViewController 
{ 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     // register for notifications when the keyboard appears: 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillChangeFrameNotification, object: nil) 
    } 
    override func viewDidLayoutSubviews() { 
     tableView.frame = view.bounds 
    } 

    lazy var tableView:UITableView = { 
     let tableView = UITableView() 
     self.view.addSubview(tableView) 
     tableView.dataSource = self.dataSource 
     tableView.delegate = self 
     return tableView 
    }() 

    lazy var dataSource : DataSource = DataSource() 

    // Handle keyboard frame changes here. 
    // Use the CGRect stored in the notification to determine what part of the screen the keyboard will cover. 
    // Adjust our table view's contentInset and scrollIndicatorInsets properties so that the table view content avoids the part of the screen covered by the keyboard 
    @objc func keyboardWillShow(note:NSNotification) 
    { 
     // read the CGRect from the notification (if any) 
     if let newFrame = (note.userInfo?[ UIKeyboardFrameEndUserInfoKey ] as? NSValue)?.CGRectValue() { 
      let insets = UIEdgeInsetsMake(0, 0, newFrame.height, 0) 
      tableView.contentInset = insets 
      tableView.scrollIndicatorInsets = insets 
     } 
    } 
} 

// need to conform to UITableViewDelegate protocol since we are the table view's delegate 
extension ViewController : UITableViewDelegate 
{ 
} 

// App set up stuff here: 
@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    lazy var window:UIWindow? = UIWindow() 


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

     window!.rootViewController = ViewController() 
     window!.makeKeyAndVisible() 
     return true 
    } 
} 
Problemi correlati