Alok Alok - 3 months ago 17
iOS Question

Why UiTextField from customTableViewCell not returning string in my UiViewController with UITableView

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

Answer

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;
}
Comments