我知道之前已经问过这个问题,但答案不明确
问题是
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
答案 0 :(得分:2)
在从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;
答案 1 :(得分:0)
-(bool)textFieldDidEndEditing:(UITextField *) textField {
[self.textFieldArray addObject:self.content];
[textField setText:@""];
[tableView reloadData]; //Reload tableview
return YES;
}
在textFieldDidEndEditing委托
中调用tableview重新加载答案 2 :(得分:0)
Vivek的答案非常好,除了你不需要重新加载表,除非它真的有必要(额外的处理时间和所有你知道的),所以不要重新加载整个表你可以只添加一行,如下所示:
- (void) textFieldDidEndEditing:(UITextField *)textField {
[_tableDSArray addObject:textField.text];
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:tableDSArray.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
}
答案 3 :(得分:0)
-(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;
}