转载请说明(谢谢)
http://blog.csdn.net/a21064346/article/details/7852684
以下是通知的收发,直接copy到你的项目中即可。应该不会有什么bug。
自己搭建 app delegate,window =viewController.View即可
//
// ViewController.h
// UIKeyboardTextNotification
//
// Created by tuxiangqi on 12-8-10.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import<UIKit/UIKit.h>
#import<Foundation/Foundation.h>
@interface ViewController :UIViewController <UITableViewDelegate,UITableViewDataSource,
UITextFieldDelegate> {
}
@property (nonatomic,strong)
UITableView *myTableView;
@end
//
// ViewController.m
// UIKeyboardTextNotification
//
// Created by tuxiangqi on 12-8-10.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import"ViewController.h"
@implementation ViewController
@synthesize myTableView;
-(void)viewDidLoad{
[superviewDidLoad];
self.myTableView
= [[UITableViewalloc]
initWithFrame:self.view.boundsstyle:UITableViewStyleGrouped];
self.myTableView.delegate
=self;
self.myTableView.dataSource
=self;
self.myTableView.autoresizingMask
=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.viewaddSubview:self.myTableView];
}
-(void)viewDidUnload{
[selfsetMyTableView:nil];
[superviewDidUnload];
}
//页面出现前,添加监听键盘事件
- (void) viewDidAppear:(BOOL)paramAnimated{
[superviewDidAppear:paramAnimated];
NSNotificationCenter *center = [NSNotificationCenterdefaultCenter];
[center
addObserver:self
selector:@selector(handleKeyboardWillShow:)
name:UIKeyboardWillShowNotification //键盘将出现事件监听
object:nil];
[center
addObserver:self
selector:@selector(handleKeyboardWillHide:)
name:UIKeyboardWillHideNotification //键盘将隐藏事件监听
object:nil];
}
- (void) viewDidDisappear:(BOOL)paramAnimated {
[superviewDidDisappear:paramAnimated];
[[NSNotificationCenterdefaultCenter]
removeObserver:self];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
/* Make sure the Done button on the keyboard for each text field (accessory views of each cell) dismisses the keyboard */
[textFieldresignFirstResponder];
return
YES;
}
- (void) handleKeyboardWillShow:(NSNotification *)paramNotification{
NSDictionary *userInfo = [paramNotification userInfo];
NSValue *animationCurveObject =[userInfo
valueForKey:UIKeyboardAnimationCurveUserInfoKey];
NSValue *animationDurationObject =[userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];
NSValue *keyboardEndRectObject =[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
NSUInteger animationCurve = 0;
double animationDuration = 0.0f;
CGRect keyboardEndRect = CGRectMake(0,0,
0, 0);
[animationCurveObjectgetValue:&animationCurve];
[animationDurationObjectgetValue:&animationDuration];
[keyboardEndRectObjectgetValue:&keyboardEndRect];
[UIViewbeginAnimations:@"changeTableViewContentInset"
context:NULL];
[UIViewsetAnimationDuration:animationDuration];
[UIViewsetAnimationCurve UIViewAnimationCurve)animationCurve];
UIWindow *window = [[[UIApplicationsharedApplication]
delegate]window];
CGRect intersectionOfKeyboardRectAndWindowRect =
CGRectIntersection(window.frame, keyboardEndRect);
CGFloat bottomInset = intersectionOfKeyboardRectAndWindowRect.size.height;
self.myTableView.contentInset =UIEdgeInsetsMake(0.0f,0.0f,bottomInset,0.0f);
NSIndexPath *indexPathOfOwnerCell = nil;
/* Also, make sure the selected text field is visible on the screen */
NSInteger numberOfCells = [self.myTableView.dataSourcetableView:self.myTableView
numberOfRowsInSection:0];
/* So let's go through all the cells and find their accessory text fields.
Once we have the refernece to those text fields, we can see which one of
them is the first responder (has the keyboard) and we will make a call
to the table view to make sure after the keyboard is displayed,
that specific cell is NOT obstructed by the keyboard */
for (NSInteger counter =
0;counter < numberOfCells;counter++){
NSIndexPath *indexPath = [NSIndexPath
indexPathForRow:counter inSection:0];
UITableViewCell *cell = [self.myTableViewcellForRowAtIndexPath:indexPath];
UITextField *textField = (UITextField *)cell.accessoryView;
if ([textField isKindOfClass:[UITextFieldclass]] ==
NO)
{
continue;
}
if ([textField isFirstResponder])
{
indexPathOfOwnerCell = indexPath;
break;
}
}
[UIViewcommitAnimations];
if (indexPathOfOwnerCell != nil){
[self.myTableViewscrollToRowAtIndexPath:indexPathOfOwnerCell
atScrollPosition:UITableViewScrollPositionMiddle
animated:YES];
}
}
- (void) handleKeyboardWillHide:(NSNotification *)paramNotification{
if (UIEdgeInsetsEqualToEdgeInsets(self.myTableView.contentInset,UIEdgeInsetsZero))
{
/* Our table view's content inset is intact so no need to reset it */
return;
}
NSDictionary *userInfo = [paramNotification userInfo];
NSValue *animationCurveObject =[userInfo
valueForKey:UIKeyboardAnimationCurveUserInfoKey];
NSValue *animationDurationObject = [userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey];
NSValue *keyboardEndRectObject =[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
NSUInteger animationCurve = 0;double animationDuration =0.0f;
CGRect keyboardEndRect = CGRectMake(0,0,
0, 0);
[animationCurveObjectgetValue:&animationCurve];
[animationDurationObjectgetValue:&animationDuration];
[keyboardEndRectObjectgetValue:&keyboardEndRect];
[UIViewbeginAnimations:@"changeTableViewContentInset"context:NULL];
[UIViewsetAnimationDuration:animationDuration];
[UIViewsetAnimationCurve UIViewAnimationCurve)animationCurve];self.myTableView.contentInset
=UIEdgeInsetsZero;[UIViewcommitAnimations];
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 100;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
UITableViewCell *result = nil;
static NSString *CellIdentifier =
@"CellIdentifier";
result = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
if (result == nil){
result = [[UITableViewCellalloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
result.selectionStyle =UITableViewCellSelectionStyleNone;
}
result.textLabel.text = [NSStringstringWithFormat:@"Cell
%ld", (long)indexPath.row];
CGRect accessoryRect = CGRectMake(0.0f,
0.0f,
150.0f,
31.0f);
UITextField *accesssory = [[UITextFieldalloc]
initWithFrame:accessoryRect];
accesssory.borderStyle =UITextBorderStyleRoundedRect;
accesssory.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;
accesssory.placeholder =@"Enter Text";
accesssory.delegate =self;
result.accessoryView = accesssory;
return result;
}
@end