简体   繁体   English

每次文本字段返回文本时,如何从UITextField加载文本并将其显示在UITableView中

[英]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. 问题是UITableview使用的是viewDidLoad方法中的数据,其中已分配并初始化了NSMuatableArray ,但其中包含void。 But in the textfield method of textFieldDidEndEditing , NSMutableArray do contain text from textField. 但是在textFieldDidEndEditing的textfield方法中, NSMutableArray确实包含来自textField的文本。

I have a single viewController and texField and tableview exist in it having dataSource set to the same viewController. 我只有一个viewController,并且存在texField和tableview,其中dataSource设置为相同的viewController。 The problem is how can I pass that value to UITableView 问题是如何将该值传递给UITableView

viewController.h 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 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. 从textfield中获取文本后,在textFieldDidEndEditing:方法中,重新加载表格视图,以便反映新值。 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 在textFieldDidEndEditing委托中重新调用tableview

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: Vivek的答案非常好,除了您不需要重新加载表,除非它确实是必要的(额外的处理时间和您所知道的全部),所以除了重新加载整个表,您还可以只添加一行,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM