如何从 UITextField 加载文本并在每次文本字段 return 文本时在 UITableView 中显示它

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

我知道这个问题以前有人问过,但答案不清楚

The problem is that theUITableview 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.

我有一个 viewController,其中存在 texField 和 tableview,其中的数据源设置为相同的 viewController。 问题是如何将该值传递给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

在您的 textFieldDidEndEditing: 方法中,从文本字段中获取文本后重新加载 table 视图,以便反映新值。这样做:

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

在 textFieldDidEndEditing 委托中调用表视图重新加载

Vivek 的回答非常好,除了你不需要重新加载 table 除非真的有必要(额外的处理时间和你所知道的),所以不要重新加载整个 table您可以只添加一行,如下所示:

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