How to load text from UITextField and show it in UITableView every time textfield return text

I am aware this question has been asked before but the answer is not clear

The problem is that the UITableview is using data from viewDidLoad method
where the NSMuatableArray has been allocated and initialised but contains void. But in the textfield method of textFieldDidEndEditing, NSMutableArray do contain text from textField.

I have a single viewController and texField and tableview exist in it having dataSource set to the same viewController.
The problem is how can I pass that value to UITableView

viewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITextField *TextField;
@property NSString *content;
@property (nonatomic, strong) NSMutableArray *textFieldArray;

@end

viewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


-(BOOL)textFieldShouldReturn:(UITextField *) textField{
    self.content = [textField text];
    NSLog(@"content is %@", self.content);
    [textField resignFirstResponder];
    return YES;
}


-(bool)textFieldDidEndEditing:(UITextField *) textField {

    [self.textFieldArray addObject:self.content];
    [textField setText:@""];
    NSLog(@"Array inside field did end editing is %@", self.textFieldArray);
    return YES;
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.textFieldArray.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    cell.textLabel.text = self.textFieldArray[indexPath.row];
    return cell;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.textFieldArray = [[NSMutableArray alloc]initWithObjects:@"add more task", nil];




    NSLog(@"box in viewDidLoad ris %@", self.textFieldArray);

    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


Source: ios

Leave a Reply