My Blog List

Android FirstAid Coding

A Small Help From a Small Heart
Powered by Blogger.

A software professional, who still likes to code, likes to blog and loves gadgets.

Monday 10 June 2013

UITextField within UITableView in iPhone

CreateCustomer.h
============= 

//
//  CreateCustomer.h
//  demo
//
//  Created by Rajendra Nayak on 10/06/13.
//  Copyright (c) 2013 rajuandroid.blogspot.com Apps. All rights reserved.
//

#import <UIKit/UIKit.h>
@interface CreateCustomer : UITableViewController<UITextFieldDelegate>{
    NSString *fName,*lName,*email,*password,*confirmPassword;
 
}

@property(nonatomic,retain) NSString *name,*phone,*email,*address;

@end


CreateCustomer.m
============= 
//
//  CreateCustomer.m
//  demo
//
//  Created by Rajendra Nayak on 10/06/13.
//  Copyright (c) 2013 rajuandroid.blogspot.com Apps. All rights reserved.
//

#import "CreateCustomer.h"


@interface CreateCustomer ()

@end

@implementation CreateCustomer
@synthesize className;
@synthesize name,phone,email,address;
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.hidesBackButton = YES;
    [self.tableView setScrollEnabled:NO];
    self.navigationItem.title=@"Customer";
    self.name=@"";self.email=@"";self.phone=@"";self.address=@"";
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0) {
        return 4;
    }else {
        return 1;
    }
   
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
   
    if ([indexPath section] == 0) {
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(7, 0, cell.contentView.bounds.size.width - 27, cell.contentView.bounds.size.height)];
        [textField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
        [textField setAutocorrectionType:UITextAutocorrectionTypeNo];
        [textField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        [textField setBorderStyle:UITextBorderStyleNone];
        [textField setClearButtonMode:UITextFieldViewModeWhileEditing];
        textField.delegate=self;
       
        if (indexPath.row == 0) {
            [textField setAutocapitalizationType:UITextAutocapitalizationTypeWords];          
            [textField setPlaceholder:@"Name"];
           
        }
        else if (indexPath.row == 1) {
            [textField setPlaceholder:@"Email"];
            [textField setKeyboardType:UIKeyboardTypeEmailAddress];
            // [textField becomeFirstResponder];
           
        }else if (indexPath.row == 2) {
            [textField setPlaceholder:@"Phone"];          
            [textField setKeyboardType:UIKeyboardTypePhonePad];
           
        } else {
            [textField setPlaceholder:@"Address"];           
            [textField setKeyboardType:UIKeyboardTypeDefault];
           
        }
        [cell.contentView addSubview:textField];
        [textField release];
    }else { // Login button section
        cell.textLabel.text = @"Create Customer";
        cell.textLabel.textAlignment=UITextAlignmentCenter;
        cell.textLabel.textColor=[UIColor brownColor];
    }
   
   
    return cell;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
   
    [textField resignFirstResponder];
   
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
    UITableViewCell *cell = (UITableViewCell *)[[textField superview] superview];
    UITableView *table = (UITableView *)[cell superview];
    NSIndexPath *textFieldIndexPath = [table indexPathForCell:cell];
    if (textFieldIndexPath.section==0) {
        if (textFieldIndexPath.row == 0) {
            self.name=textField.text;
        }
        else if (textFieldIndexPath.row == 1) {
            self.email=textField.text;
        }else if (textFieldIndexPath.row == 2) {
            self.phone=textField.text;
        }else if (textFieldIndexPath.row == 3) {
            self.address=textField.text;
        }
    }
   
   
   
}
-(IBAction)registerButtonPressed{
    [self.view endEditing:YES];
    NSLog(@"name==%@",self.name);       
}
@end




1 comment: