I am aware this question has been asked before but the answer is not clear
The problem is that the
UITableview
is using data fromviewDidLoad
method where theNSMuatableArray
has been allocated and initialised but contains void. But in the textfield method oftextFieldDidEndEditing
,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
In your textFieldDidEndEditing:
method after fetching the text from textfield reload the table view so that the new value get reflected. Do it this way:
[self.textFieldArray addObject:self.content];
[textField setText:@""];
// Your code is missing the following line
[tableView reloadData]; // tableView is instance of your table view
NSLog(@"Array inside field did end editing is %@", self.textFieldArray);
return YES;
-(bool)textFieldDidEndEditing:(UITextField *) textField {
[self.textFieldArray addObject:self.content];
[textField setText:@""];
[tableView reloadData]; //Reload tableview
return YES;
}
Call tableview reload in textFieldDidEndEditing delegate
Vivek's Answer is perfectly fine, except that fact you don;t need to reload table unless it's really necessary (extra processing time and all you know), so instead of reloading entire table you can add just one row, like this:
- (void) textFieldDidEndEditing:(UITextField *)textField {
[_tableDSArray addObject:textField.text];
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:tableDSArray.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
}
-(bool)textFieldDidEndEditing:(UITextField *) textField {
[self.textFieldArray addObject:self.content];
[textField setText:@""];
[tableView setdelegate:nil];
[tableView setdatasource:nil];
[tableView setdelegate:self];
[tableView setdatasource:self];
[tableView reloadData];
NSLog(@"Updated With Entered Text:\n%@",self.textFieldArray);
return YES;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.