簡體   English   中英

每次文本字段返回文本時,如何從UITextField加載文本並將其顯示在UITableView中

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

我知道之前曾問過這個問題,但答案尚不清楚

問題是UITableview使用的是viewDidLoad方法中的數據,其中已分配並初始化了NSMuatableArray ,但其中包含void。 但是在textFieldDidEndEditing的textfield方法中, NSMutableArray確實包含來自textField的文本。

我只有一個viewController,並且存在texField和tableview,其中dataSource設置為相同的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

從textfield中獲取文本后,在textFieldDidEndEditing:方法中,重新加載表格視圖,以便反映新值。 這樣做:

[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委托中重新調用tableview

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