選擇文本字段時生成UITableView滾動框經(jīng)過多次嘗試和錯誤之后,我放棄了并問了這個問題。我見過很多人都有類似的問題,但無法找到正確的答案。我有一個UITableView由自定義單元格組成。單元格由相鄰的5個文本字段組成(有點像網(wǎng)格)。當(dāng)我試圖滾動和編輯底部的單元格時,UITableView,我無法使我的細(xì)胞正確地定位在鍵盤上方。我見過很多關(guān)于改變視野大小等問題的答案.但到目前為止,他們中沒有一個人工作得很好。有誰能用一個具體的代碼示例來澄清做這件事的“正確”方法嗎?
3 回答

白板的微信
TA貢獻(xiàn)1883條經(jīng)驗 獲得超3個贊
- (void) textFieldDidBeginEditing:(UITextField *)textField { UITableViewCell *cell; if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) { // Load resources for iOS 6.1 or earlier cell = (UITableViewCell *) textField.superview.superview; } else { // Load resources for iOS 7 or later cell = (UITableViewCell *) textField.superview.superview.superview; // TextField -> UITableVieCellContentView -> (in iOS 7!)ScrollView -> Cell! } [tView scrollToRowAtIndexPath:[tView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];}

繁華開滿天機(jī)
TA貢獻(xiàn)1816條經(jīng)驗 獲得超4個贊
@interface MyUIViewController: UIViewController <UITableViewDelegate, UITableViewDataSource>{ UITableView *myTableView; UITextField *actifText;}@property (nonatomic, retain) IBOutlet UITableView *myTableView;@property (nonatomic, retain) IBOutlet UITextField *actifText;- (IBAction)textFieldDidBeginEditing:(UITextField *)textField;- (IBAction)textFieldDidEndEditing:(UITextField *)textField;-(void) keyboardWillHide:(NSNotification *)note;-(void) keyboardWillShow:(NSNotification *)note;@end
@implementation MyUIViewController@synthesize myTableView;@synthesize actifText;- (void)viewDidLoad { // Register notification when the keyboard will be show [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; // Register notification when the keyboard will be hide [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];}// To be link with your TextField event "Editing Did Begin"// memoryze the current TextField- (IBAction)textFieldDidBeginEditing:(UITextField *)textField{ self.actifText = textField;}// To be link with your TextField event "Editing Did End"// release current TextField- (IBAction)textFieldDidEndEditing:(UITextField *)textField{ self.actifText = nil;}-(void) keyboardWillShow:(NSNotification *)note{ // Get the keyboard size CGRect keyboardBounds; [[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds]; // Detect orientation UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; CGRect frame = self.myTableView.frame; // Start animation [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:0.3f]; // Reduce size of the Table view if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) frame.size.height -= keyboardBounds.size.height; else frame.size.height -= keyboardBounds.size.width; // Apply new size of table view self.myTableView.frame = frame; // Scroll the table view to see the TextField just above the keyboard if (self.actifText) { CGRect textFieldRect = [self.myTableView convertRect:self.actifText.bounds fromView:self.actifText]; [self.myTableView scrollRectToVisible:textFieldRect animated:NO]; } [UIView commitAnimations];}-(void) keyboardWillHide:(NSNotification *)note{ // Get the keyboard size CGRect keyboardBounds; [[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds]; // Detect orientation UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; CGRect frame = self.myTableView.frame; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:0.3f]; // Increase size of the Table view if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) frame.size.height += keyboardBounds.size.height; else frame.size.height += keyboardBounds.size.width; // Apply new size of table view self.myTableView.frame = frame; [UIView commitAnimations];}@end
SWIFT 1.2+版本:
class ViewController: UIViewController, UITextFieldDelegate { @IBOutlet weak var activeText: UITextField! @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil) } func textFieldDidBeginEditing(textField: UITextField) { activeText = textField } func textFieldDidEndEditing(textField: UITextField) { activeText = nil } func keyboardWillShow(note: NSNotification) { if let keyboardSize = (note.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() { var frame = tableView.frame UIView.beginAnimations(nil, context: nil) UIView.setAnimationBeginsFromCurrentState(true) UIView.setAnimationDuration(0.3) frame.size.height -= keyboardSize.height tableView.frame = frame if activeText != nil { let rect = tableView.convertRect(activeText.bounds, fromView: activeText) tableView.scrollRectToVisible(rect, animated: false) } UIView.commitAnimations() } } func keyboardWillHide(note: NSNotification) { if let keyboardSize = (note.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() { var frame = tableView.frame UIView.beginAnimations(nil, context: nil) UIView.setAnimationBeginsFromCurrentState(true) UIView.setAnimationDuration(0.3) frame.size.height += keyboardSize.height tableView.frame = frame UIView.commitAnimations() } }}
- 3 回答
- 0 關(guān)注
- 327 瀏覽
添加回答
舉報
0/150
提交
取消