Search code examples
iosobjective-cxcodeuitableviewreloaddata

doesn't work reloadData on UITableViewController


I want add data from a textField into a UITableViewController. Delegate and transfer data works fine. My mutableArray is modifying, he is looking corectly but tableView is not reloading.

tableViewController.h
#import <UIKit/UIKit.h>
#import "ViewController.h"

@interface TableViewController : UITableViewController <UITableViewDelegate,UITableViewDataSource, trecereDateDelegate>
@property (strong, nonatomic) NSMutableArray *todoitems;

tableViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    _todoitems = [[NSMutableArray alloc]initWithArray:[NSArray arrayWithObjects:@"abc",@"adasda",@"asdadq", nil]];
}
- (void)trans

ferData:(NSString *)textNou{
        [_todoitems addObject:textNou];
        [self.tableView reloadData];

    }- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

        return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        return _todoitems.count;
    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableCell" forIndexPath:indexPath];

        NSInteger row=[indexPath row];
        cell.afiseaza.text = _todoitems[row];
        // Configure the cell...


        return cell;
    }

ViewController.h

@class ViewController;

@protocol trecereDateDelegate <NSObject>

-(void) transferData:(NSString *)data;

@end

@interface ViewController : UIViewController <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *introdu;

- (IBAction)returnButton:(id)sender;
- (IBAction)saveButton:(id)sender;

@property (weak, nonatomic) id<trecereDateDelegate>delegate;


@end

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    self.introdu.delegate = self;

}

- (IBAction)saveButton:(id)sender{



    NSString *ceva=_introdu.text;
    [_delegate transferData:ceva];

    TableViewController *xyz=[[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"LISTA"];


    [self showViewController:xyz sender:nil];

}

Solution

  • try this: Add a NSString object to fetch the result entered into text field.

    TableViewController.h

    @interface TableTableViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate>
    
    @property (strong, nonatomic) NSMutableArray *todoitems;
    
    @property (strong,nonatomic) NSString *textFieldString;
    
    @end
    

    TableViewController.m

    #import "TableTableViewController.h"
    
    @interface TableTableViewController ()
    
    @end
    
    @implementation TableTableViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        _todoitems = [[NSMutableArray alloc]initWithArray:[NSArray arrayWithObjects:@"abc",@"adasda",@"asdadq", nil]];
    //add object passed for TextField.
        [_todoitems addObject:_textFieldString];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    -(void) transferData:(NSString *)textNou{
        [_todoitems addObject:textNou];
        [self.tableView reloadData];
    }
    
    #pragma mark - Table view data source
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        // Return the number of sections.
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
        // Return the number of rows in the section.
         return _todoitems.count;
    }
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        // Configure the cell...
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableCell" forIndexPath:indexPath];
    
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tableCell"];
        }
    
        NSInteger row=[indexPath row];
        cell.textLabel.text = _todoitems[row];
        // Configure the cell...
    
        return cell;    
    }
    

    ViewController.h

    @protocol trecereDateDelegate <NSObject>
    
    -(void) transferData:(NSString *)data;
    
    @end
    
    @interface ViewController : UIViewController<UITextFieldDelegate>
    
    @property (weak, nonatomic) IBOutlet UITextField *introdu;
    
    - (IBAction)returnButton:(id)sender;
    
    - (IBAction)saveButton:(id)sender;
    
    @property (weak, nonatomic) id<trecereDateDelegate>delegate;
    
    @end
    

    ViewController.m

    #import "ViewController.h"
    #import "TableTableViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
         self.introdu.delegate = self;
    
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (IBAction)saveButton:(id)sender{
    
    
        NSString *ceva=_introdu.text;
    //    [_delegate transferData:ceva];
    
        TableTableViewController *xyz=[[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"LISTA"];
    
       // just pass the string from textfield to tableViewController
       xyz.textFieldString = ceva;
    
        [self showViewController:xyz sender:nil];
    
    }
    

    you would not need delegate to pass data to another view controller.