1

My project has a custom UITableViewCell with an UIImage and an UITextField.

In a view controller with a UITableView, I can access and set the customCell property from ViewController UITableView using datasource and delegate protocol. The problem is when want to NSLog UITextField text using method textFieldShouldReturn. Below is my code and snapshot of tableview in viewController.

CustomModalTableViewCell

#import <UIKit/UIKit.h>

@interface CustomModalTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UITextField *customCellTextField;
@property (strong, nonatomic) IBOutlet UIImageView *customCellImageView;
@end

ModalViewController.h

  #import <UIKit/UIKit.h>

@interface ModalViewController : UIViewController<UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate>

@end

ModalViewController.m

#import "ModalViewController.h"
#import "CustomModalTableViewCell.h"
#import "MYImageClass"

@interface ModalViewController (){
    CustomModalTableViewCell *customCell;
    NSString *bookFieldString;
}

@property (strong, nonatomic) IBOutlet UITableView *modalTable;
@end

@implementation ModalViewController

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    NSLog(@"book textFiled value is %@", customCell.customCellTextField.text);

    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    customCell.customCellTextField.delegate = self;
    self.modalTable.delegate = self;
    self.modalTable.dataSource = self;
  }

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (section == 0) {
        return  2;
    }else{
        return 1;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 20;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
         return @"Book Details";
    }else if (section == 1){
         return @"Reading Details";
    }
    return @"ok";

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CustomModalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customCellForModal"];
    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"customModalTableViewCell" bundle:nil] forCellReuseIdentifier:@"customCellForModal"];
        cell = [tableView dequeueReusableCellWithIdentifier:@"customCellForModal"];
    }
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.customCellImageView.image = [MYImageClass imageOfBookIcon];
            cell.customCellTextField.placeholder = @"Enter book name here";

        }if (indexPath.row == 1) {
            cell.customCellImageView.image = [MYImageClas imageOfAuthorIcon];
            cell.customCellTextField.placeholder = @"Enter author name here";
        }
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            cell.customCellImageView.image = [MYImageClas imageOfTotalPageIcon];
            cell.customCellTextField.placeholder = @"Enter total page here";
        }
    }

       return cell;
}


@end
NSNoob
  • 5,548
  • 6
  • 41
  • 54
Alok
  • 119
  • 1
  • 9
  • 1
    Add this lines to `cellForRowAtIndexPath`: `cell.customCellTextField.delegate = self;` In textfield delegate, remove your existing code, just add this one `NSLog(@"book textFiled value is %@", textField.text);` Also, why the global instance of cell? You are not using it to populate `tableView` – NSNoob Nov 11 '15 at 06:08
  • Thanks for prompt reply, I was setting textField delegate in viewDidLoad. Your answer did work. – Alok Nov 11 '15 at 06:14
  • Responding to your further request (Which you have since deleted apparently), Change keyboard type in the conditions where you set the placeholder text for textfield. Like this. http://paste.ubuntu.com/13225287/ – NSNoob Nov 11 '15 at 07:00
  • User can still paste copied string in number only UITextField. I do have to use tag property to make it first responder alternatively. – Alok Nov 11 '15 at 07:08
  • You can disable pasting in textfield like this http://stackoverflow.com/questions/6701019/how-to-disable-copy-paste-option-from-uitextfield-programmatically. But yes you can also set tags of the textfield equal to indexPath.section and use those tags to analyze string before returning and therefore reject/accept input based on that – NSNoob Nov 11 '15 at 08:08

1 Answers1

1

The problem with your code is, you are confused about the concept of delegates and tableviews.

a. First off Get rid of that global variable named customCell. You don't need that and you are not using that.

b. In your cellForRowAtIndexPath method, type this line after declaring the cell.

cell.customCellTextField.delegate = self;

c. Replace your delegate method for textField, textFieldShouldReturn with this:

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
   NSLog(@"book textFiled value is %@", textField.text);

   return YES;
}
NSNoob
  • 5,548
  • 6
  • 41
  • 54