0

I am developing an iPhone application, in a view, I have placed UITextView and UITableView in the same ViewController. What i need to do is, after entering the data in the textview, the user will click on a button placed below. After clicking on the button, the data entered in the textview should be loaded in the tableview. But it is not working so.

Here, what i have done is, I have written the below code inside the button action,

[self.tableView reloadData];

Any suggestions much appreciated

Cœur
  • 37,241
  • 25
  • 195
  • 267
jireh
  • 89
  • 1
  • 2
  • 11

2 Answers2

3

Make sure you are updating the NSMutableArray with the new value, that you are using to fill data in your UITableView.

On the click of the button,

[arrValues addObject:txtBox.text];
[self.tableView reloadData];
The iOSDev
  • 5,237
  • 7
  • 41
  • 78
Kanan Vora
  • 2,124
  • 1
  • 16
  • 26
  • Yes, i am adding the array with the value i am entering. Because, after entering data and button click, I just navigate to previous view and then to same view displays the added data in the UITableView but it is not loaded in the same view itself. – jireh Oct 09 '12 at 07:03
  • Check properly which table exactly you are reloading after entering new value, present view's table or previous view's table? – Kanan Vora Oct 09 '12 at 07:05
  • Then make sure you are returning correct number of rows in numberOfRowsInSection. You shud put ur code. Then it can be easy finding your problem. – Kanan Vora Oct 09 '12 at 07:18
  • In ViewDidLoad, I am fetching the data from the URL and adding the contents return from the url in the array (NSMutable). And, I have written [array count] in the numberOfRowsInSection, and it is working fine. On the button click (function), I am just passing the entered value in the textview to the URL (php) and it is also working fine and data is also added to the same url. But, the value doesn't display in the tableview in the same instance. But it displays correctly when i go to the previous view and get into the same view again, the value is fetched from the url and it displays correctly – jireh Oct 09 '12 at 07:32
  • And i have added, [self.tableView reloadData], in the ViewDidLoad, ViewwillAppear and also inside the button action. help me. – jireh Oct 09 '12 at 07:33
1

This method handle your rowCount of TableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [arrayData count];
}

And this method will make your cell and showing cell data.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

So you will have to add textField.text in that array which you're using for numberOfRowsInSection: and cellForRowAtIndexPath:

-(void)buttonClick:(id)sender
 {
     [arrayData addObject:textField.text];
     [self.tableView reloadData];
 }

And make sure textField.text should not be empty other wise data will not be visible. After calling [self.tableView reloadData] method all tableView delegates method will be call again and arrayData will increase the numberOfRows for your tableView.

EDIT: As you're saying in comment that you're navigating the view so each time your viewDidLoad: will be call and your array will have the same object which your initialize in viewDidLoad: if you want that your array remain same objects then dont initialize it static, use plist or Database and save your array data in Database in viewDidDisappear: method and refresh your array in viewWillAppear: method before adding your tableView.

TheTiger
  • 13,264
  • 3
  • 57
  • 82