1

I am making an app that is using core data, and I have 2 textfields and a button saying "save data".So I want when the button is pressed the data from the textfields is displayed on a new row in a table view controller.In Xcode using Objective C.

Image of views - https://i.stack.imgur.com/7UrMO.png

Thanks Beginner to programming

3 Answers3

3

Follow these steps:

Step 1: Update your model (used to draw your cell) with textFields text. Something like this:

[self.items addObject:<Object_With_Both_TextFields_Text>]

Step 2: On button tap, insert a new row like this:

    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:self.items.count - 1 inSection:0];
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationBottom];
    [self.tableView endUpdates];
Abhinav
  • 37,684
  • 43
  • 191
  • 309
  • Where do I enter this code in the view controller or table view controller and where in those files ( .h or .m). – Muhammed Mafawalla Oct 09 '15 at 00:02
  • This should go in .m of your 'UITableViewControler' delegate class. – Abhinav Oct 09 '15 at 00:46
  • Could you please throw more light on how your views are laid down. Because the kind of questions you are asking is really confusing me. I suggested an approach to you thinking you could follow it. But if you are looking answer for specific questions, you would need to update your question with more details including code on how your views are laid down. I was expecting you had one view controller with top having text fields and bottom having table view in which case your text fields are managed by same view controller. – Abhinav Oct 09 '15 at 03:36
  • Sorry for my vague question,I have added a picture of my view controllers in the original question.I want the "Name" and "Age" input to be displayed in a new row on the table view controller. – Muhammed Mafawalla Oct 09 '15 at 05:47
2

Just add the new data from text fields to your table view data source (mostly this will be a mutable array or dictionary). Then call reloadData on your table view.

  1. Add the entry from text fields into your table view data source.

    [yourMutableArray addObject:[textField1 text]];
    
  2. call [yourTableView reloadData];

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
Rashmi Ranjan mallick
  • 6,390
  • 8
  • 42
  • 59
  • As per my opinion its not good idea to reload whole tableview while inserting new row its better to use its DataSource inserRowAtIndexpath: – Piyush Oct 08 '15 at 11:15
  • Is not the best solution but it works very well. There is not only one solution. Normally I use this way and never problems. – Lorenzo Oct 08 '15 at 11:40
  • @PiyushPatel: While I agree with your point, there's no harm in "Reloading the table view" in case of small updates. And in this question, Muhammed Mafawalla asked about loading contents from two text fields, which I feel is minor updates. Hope I make sense!! – Rashmi Ranjan mallick Oct 09 '15 at 04:52
  • 1
    @RashmiRanjanmallick : Yes you are right, I am agree with you. – Piyush Oct 09 '15 at 05:17
1

When you press 'save data', add content that you want to display into the your_array that you are using to display the tableview.

[your_array addObject:your_textfield_string];

After that reload the table again using [tableview reloadData]:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [tableview reloadData];

                dispatch_async(dispatch_get_main_queue(), ^{

                    ////TO SCROLL THE TABLE TO NEWLY ADDED ROW
                    int lastRowNumber = [tableview numberOfRowsInSection:0] - 1;

                    NSIndexPath* ip = [NSIndexPath indexPathForRow:lastRowNumber inSection:0];
                    [tableview scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:NO];
                });

            });
Rumin
  • 3,787
  • 3
  • 27
  • 30