-3

Note for the person who downvoted: This question was closed so many days back . If anyone find which is not necessary please leave a comment

I have some textfield values & uiimage stored in one view controller. I have tableview in another view controller . I want store values using nsuserdefaults in tableview and retrieve using nsuserdefaults then want to add rows dynamically as user enters?

Uma Madhavi
  • 4,851
  • 5
  • 38
  • 73

3 Answers3

1

You can maintain a Mutable array with strings from textfield.

Then based on the array.count you can get the size of array.

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
  return array.count
}

and also don't forget to reload table view.

Piyush Mathur
  • 1,617
  • 16
  • 33
  • @Uma does your array count gets updated when you come to your `tableview`? – Rumin Oct 30 '15 at 10:28
  • Do you add the new cell's data to the array before calling the [tableView relaodData] method? – NSNoob Oct 30 '15 at 10:28
  • @NSNoob i am unable to add to new cell . – Uma Madhavi Oct 30 '15 at 10:30
  • @Uma Then, there is a problem with your array. You are not adding the objects into the array of textfield values correctly. If array count is not getting updated then how will data be shown correctly? – Rumin Oct 30 '15 at 10:32
1

Let's say you have an NSMutableArray of this data like this:

NSMutableArray *cellLabelValues = [[NSMutableArray alloc] initWithObjects: @"1",@"2",@"3",nil];

In your tableView delegates do this:

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
  return cellLabelValues.count;
}

You must have a button or something to receive user's intent to add new cells. Lets assume it calls this method:

   -(void) addNewCell:
    {
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter New Value"
                                                message:@"  "   
                                               delegate:self 
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
    }

And write the delegate of UIAlertView. Make sure you have made your ViewController to be UIAlertViewDelegate:

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        if (buttonIndex == 1) {
            NSString *value = [alertView textFieldAtIndex:0].text;
            [cellLabelValues addObject:value];
            [tableView reloadData];
    }
    }

What happens here is, user taps new cell button. It takes entry from user. Then it reloads the table after adding it to your array. When it reaches numberOfRowsInSection delegate method, it sees that the array is now incremented by 1. So it will show one extra cell.

NSNoob
  • 5,548
  • 6
  • 41
  • 54
  • Then you will have to use `UITextField`. Use it to get text from user. When the entry ends, add it to the array and reload the tableview – NSNoob Oct 30 '15 at 10:39
  • I have just edited the code. Now it will take value from the user on runtime and show it in table when you press okay – NSNoob Oct 30 '15 at 10:44
0

Suppose you have two UIViewControllers ViewController1 with UITextFields and ViewController2 with UITableView

Now in ViewController2 declare an NSMutableArray *dataArray;

and add record in it from ViewController1 before moving to ViewController2

In ViewController2 use this function

 - (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
  return dataArray.count;
}

It will help.

baydi
  • 1,003
  • 6
  • 11