The below are customcell file
//.h file
#import <UIKit/UIKit.h>
@protocol TextFieldScrollToVisibleDelegate
@optional
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
- (void)textFieldDidBeginEditing:(UITextField *)textField;
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
- (void)textFieldDidEndEditing:(UITextField *)textField;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
- (BOOL)textFieldShouldClear:(UITextField *)textField;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;
- (void)textViewDidBeginEditing:(UITextView *)textView;
- (void)textViewDidEndEditing:(UITextView *)textView;
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
- (void)textViewDidChange:(UITextView *)textView;
- (void)textViewDidChangeSelection:(UITextView *)textView;
@end
@interface CustomTableViewCell : UITableViewCell <UITextFieldDelegate,UITextViewDelegate>
@property (nonatomic, assign) NSObject <TextFieldScrollToVisibleDelegate> *mTextFieldScrollToVisibleDelegate_;
@property (nonatomic) int paddingabovekeyboard;
@end
//.m file
#import "CustomTableViewCell.h"
@interface CustomTableViewCell ()
@property (nonatomic)int keyboardHeight;
@property (nonatomic)BOOL useSecondMode;
@property (nonatomic)int actualViewHeight;
@property (nonatomic)int actualViewWidth;
@property (nonatomic)int previousYposition;
@end
#define kKeyboardHeightValue (keyboardHeight==0)?250:keyboardHeight
@implementation CustomTableViewCell
@synthesize mTextFieldScrollToVisibleDelegate_;
@synthesize paddingabovekeyboard;
@synthesize keyboardHeight;
@synthesize useSecondMode;
@synthesize actualViewHeight;
@synthesize actualViewWidth;
@synthesize previousYposition;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
paddingabovekeyboard=50;
}
return self;
}
-(void)wheatherFirstModeOrsecondMode {
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
BOOL statusBarHidden = [UIApplication sharedApplication].statusBarHidden;
int statusHight= 20;
if(statusBarHidden) {
statusHight=0;
}
int navheight=0;
id nav = [UIApplication sharedApplication].keyWindow.rootViewController;
if ([nav isKindOfClass:[UINavigationController class]]) {
UINavigationController *navc = (UINavigationController *) nav;
if(!navc.navigationBarHidden) {
navheight=44;
}
}
if(UI_USER_INTERFACE_IDIOM () ==UIUserInterfaceIdiomPhone )
{
if(orientation == 0||orientation == UIInterfaceOrientationPortrait) {
keyboardHeight = 216+paddingabovekeyboard; //44
actualViewHeight= [[UIScreen mainScreen]bounds].size.height-statusHight-navheight;
actualViewWidth =[[UIScreen mainScreen]bounds].size.width;
} else if(orientation == UIInterfaceOrientationLandscapeLeft||orientation == UIInterfaceOrientationLandscapeRight) {
keyboardHeight = 162+paddingabovekeyboard; //444
actualViewHeight= [[UIScreen mainScreen]bounds].size.width-statusHight-navheight;
actualViewWidth =[[UIScreen mainScreen]bounds].size.height;
}
} else if (UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiomPad){
if(orientation == 0||orientation == UIInterfaceOrientationPortrait) {
keyboardHeight = 264+paddingabovekeyboard; //44
actualViewHeight= [[UIScreen mainScreen]bounds].size.height-statusHight-navheight;
actualViewWidth =[[UIScreen mainScreen]bounds].size.width;
} else if(orientation == UIInterfaceOrientationLandscapeLeft||orientation == UIInterfaceOrientationLandscapeRight) {
keyboardHeight = 352+paddingabovekeyboard;//44
actualViewHeight= [[UIScreen mainScreen]bounds].size.width-statusHight-navheight;
actualViewWidth =[[UIScreen mainScreen]bounds].size.height;
}
}
UITableView *myTableView = (UITableView *)[self superview];
if(![myTableView isKindOfClass:[UITableView class]]) {
myTableView = (UITableView *)[[self superview] superview];
}
if(![myTableView isKindOfClass:[UITableView class]]) {
myTableView = (UITableView *)[[[self superview] superview] superview];
}
NSLog(@"keyboardHeight %dactualViewHeight%d",keyboardHeight,actualViewHeight);
if(myTableView.frame.origin.y>actualViewHeight-keyboardHeight-50) {
useSecondMode=TRUE;
} else {
useSecondMode=FALSE;
}
#undef kKeyboardHeightValue
#define kKeyboardHeightValue (keyboardHeight==0)?250:keyboardHeight
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
@try {
UITableView *myTableView = (UITableView *)[self superview];
if(![myTableView isKindOfClass:[UITableView class]]) {
myTableView = (UITableView *)[[self superview] superview];
}
if(![myTableView isKindOfClass:[UITableView class]]) {
myTableView = (UITableView *)[[[self superview] superview] superview];
}
int scrollpadding;
if(!self.useSecondMode) {
scrollpadding=actualViewHeight - (myTableView.frame.origin.y+myTableView.frame.size.height);
scrollpadding = kKeyboardHeightValue-scrollpadding;
} else {
CGRect lframe= myTableView.superview.frame;
self.previousYposition=myTableView.superview.frame.origin.y;
int heighttochange=0;
if(myTableView.frame.origin.y<myTableView.frame.size.height) {
heighttochange = myTableView.frame.size.height;
} else {
heighttochange = myTableView.frame.origin.y;
}
int temp = actualViewHeight- keyboardHeight;
temp = self.frame.origin.y - temp+50;
heighttochange = (heighttochange>temp)?temp:heighttochange;
lframe.origin.y-=heighttochange;
self.superview.frame=lframe;
scrollpadding =actualViewHeight - ((myTableView.frame.origin.y+lframe.origin.y)+myTableView.frame.size.height);
scrollpadding = kKeyboardHeightValue-scrollpadding;
}
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, scrollpadding, 0.0);
myTableView.contentInset = contentInsets;
myTableView.scrollIndicatorInsets = contentInsets;
CGRect lFrame=[myTableView convertRect:textField.bounds fromView:textField];
[myTableView scrollRectToVisible:lFrame animated:NO];
if (nil != mTextFieldScrollToVisibleDelegate_ && [mTextFieldScrollToVisibleDelegate_ respondsToSelector:@selector(textFieldDidBeginEditing:)]) {
[mTextFieldScrollToVisibleDelegate_ textFieldDidBeginEditing:textField];
}
}
@catch (NSException *exception) {
}
@finally {
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
@try {
UITableView *myTableView = (UITableView *)[self superview];
if(![myTableView isKindOfClass:[UITableView class]]) {
myTableView = (UITableView *)[[self superview] superview];
}
if(![myTableView isKindOfClass:[UITableView class]]) {
myTableView = (UITableView *)[[[self superview] superview] superview];
}
if(!useSecondMode) {
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
myTableView.contentInset = contentInsets;
myTableView.scrollIndicatorInsets = contentInsets;
} else {
CGRect lframe= self.superview.frame;
lframe.origin.y=self.previousYposition;
self.superview.frame=lframe;
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
myTableView.contentInset = contentInsets;
myTableView.scrollIndicatorInsets = contentInsets;
}
if (nil != mTextFieldScrollToVisibleDelegate_ && [mTextFieldScrollToVisibleDelegate_ respondsToSelector:@selector(textFieldDidEndEditing:)]) {
[mTextFieldScrollToVisibleDelegate_ textFieldDidEndEditing:textField];
}
}
@catch (NSException *exception) {
}
@finally {
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
@try {
UITableView *myTableView = (UITableView *)[self superview];
if(![myTableView isKindOfClass:[UITableView class]]) {
myTableView = (UITableView *)[[self superview] superview];
}
if(![myTableView isKindOfClass:[UITableView class]]) {
myTableView = (UITableView *)[[[self superview] superview] superview];
}
UITableViewCell *cell =((UITableViewCell*)[[textField superview] superview]);
if(![cell isKindOfClass:[UITableViewCell class]]) {
cell = (UITableViewCell *)[[[textField superview]superview] superview];
}
if(!useSecondMode) {
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
myTableView.contentInset = contentInsets;
myTableView.scrollIndicatorInsets = contentInsets;
} else {
CGRect lframe= self.superview.frame;
lframe.origin.y=self.previousYposition;
self.superview.frame=lframe;
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
myTableView.contentInset = contentInsets;
myTableView.scrollIndicatorInsets = contentInsets;
}
[textField resignFirstResponder];
if (nil != mTextFieldScrollToVisibleDelegate_ && [mTextFieldScrollToVisibleDelegate_ respondsToSelector:@selector(textFieldShouldReturn:)]) {
return [mTextFieldScrollToVisibleDelegate_ textFieldShouldReturn:textField];
}
return YES;
}
@catch (NSException *exception) {
}
@finally {
}
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
if (nil != mTextFieldScrollToVisibleDelegate_ && [mTextFieldScrollToVisibleDelegate_ respondsToSelector:@selector(textFieldShouldEndEditing:)]) {
return [mTextFieldScrollToVisibleDelegate_ textFieldShouldEndEditing:textField];
}
return YES;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
[self wheatherFirstModeOrsecondMode];
if (nil != mTextFieldScrollToVisibleDelegate_ && [mTextFieldScrollToVisibleDelegate_ respondsToSelector:@selector(textFieldShouldBeginEditing:)]) {
return [mTextFieldScrollToVisibleDelegate_ textFieldShouldBeginEditing:textField];
}
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (nil != mTextFieldScrollToVisibleDelegate_ && [mTextFieldScrollToVisibleDelegate_ respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
return [mTextFieldScrollToVisibleDelegate_ textField:textField shouldChangeCharactersInRange:range replacementString:string];
}
return YES;
}
@end
Add the above two custom cell files to your project
Now the below is how to implement
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
UITextField *ltext = [[UITextField alloc] initWithFrame:CGRectMake(10, 2, 120, 30)];
ltext.borderStyle = UITextBorderStyleLine;
ltext.delegate=cell;
cell.mTextFieldScrollToVisibleDelegate_=self;
ltext.tag=1;
[cell.contentView addSubview:ltext];
}
cell.paddingabovekeyboard=44;
return cell;
}
Explanation for the above code
Use CustomTableViewCell instead of UITableViewCell.
Note: this code works only for textfields in the tableview