简体   繁体   中英

How to reload the data in UITableView in iPhone in the same view

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

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];

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM