从1页保存数据并将该数据添加到tableView [英] Save data from 1 page and add that data to a tableView
问题描述
嘿伙计我有2个视图,第一个有UITableView。第二个有一个textField,当用户按下第二页上的保存按钮时,我希望将textFields文本添加到tableView中。这是我正在使用的代码
Hey guys i have 2 views the first has a UITableView. The Second Has a textField and when the user presses a "Save" button on the second page, i want the textFields text to be added to the tableView. Here is the code i'm using
- (IBAction)saveButton:(id)sender {
CheckListPracticeViewController * obj = [[CheckListPracticeViewController alloc]init];
[obj.cells insertObject:textField.text atIndex:0];
[self dismissModalViewControllerAnimated:YES];
NSLog(@"%@ "[cells objectAtIndex:0]);
[obj.myTableView reloadData];}
由于某种原因,数据未被添加到表查看有没有人知道什么是错的? NSLog也不适用于这种方法。非常感谢大家:D
For some reason the data isnt being added to the table View does anybody know whats wrong?? Also the NSLog doesnt Work in this method. Thanks a lot Guys :D
推荐答案
那是因为你正在创建 CheckListPracticeViewController的新实例
并更新它而不是当前以模态方式呈现此视图控制器的那个。
That is because you are creating a new instance of CheckListPracticeViewController
and updating it rather than the current one which has presented this view controller modally.
将第一行更改为,
CheckListPracticeViewController * obj = (CheckListPracticeViewController *)self.parentViewController;
编辑
首先要与您的数据模型保持一致。如果你从plist加载一个字典数组,然后在该数组中添加字符串,那么你就会遇到严重的问题。我建议您创建一个包含名称和其他内容的字典对象,并将其添加到数组中。我会说 [obj.cells insertObject:[NSDictionary dictionaryWithObject:textField.text forKey:@name] atIndex:0];
而不是 [obj.cells insertObject:textField.text atIndex:0];
将修复此当前错误,但我怀疑这会解决您的问题。
First of all be consistent with your data model. If you are loading an array of dictionaries from the plist and later adding strings into that array then you have a serious problem. I will suggest that you create a dictionary object with name and other stuff and add that to the array. I would say doing [obj.cells insertObject:[NSDictionary dictionaryWithObject:textField.text forKey:@"name"] atIndex:0];
instead of [obj.cells insertObject:textField.text atIndex:0];
will fix this current error but I doubt that will fix your problem.
这篇关于从1页保存数据并将该数据添加到tableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!