tahia ata
tahia ata

Reputation: 23

Reload UITableView To Contain Newly inserted Data

I have a SecondViewController as sliding panel which has a UITableView and populates it with NSArray data returned from "getnotes" function from ViewController.Running for the first time it gets all the data but when new data is inserted the UITableView does not update though the NSArray "n" contains new data.

here is the SecondViewController.h-

     #import <UIKit/UIKit.h>
     @interface SecondViewController:UIViewController<UITableViewDelegate,UITableViewDataSource>
     {
       NSArray *n;

      }
     @property (strong, nonatomic) IBOutlet UITableView *tableview;
     -(void)gets;
     @end

Here is the SecondViewController.m-

#import "SecondViewController.h"
#import "ViewController.h"


@interface SecondViewController ()


@end

@implementation SecondViewController

- (void)viewDidLoad {
[super viewDidLoad];
[self gets];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma Table View Methods

-(void)gets{

ViewController *dba=[[ViewController alloc]init];
n = [NSArray arrayWithArray:[dba getnotes]];
 
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return 1;
}

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

if(n==nil)return 0;

return n.count;

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

     static NSString *cellID=@"cellID";
     UITableViewCell *cell=[tableView  dequeueReusableCellWithIdentifier:cellID];
     if(cell==nil){
           cell=[[UITableViewCell  alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
                  }

    NSDictionary * res =[n objectAtIndex:indexPath.row];
    

    cell.textLabel.text=[res objectForKey:@"NoteText"];
    cell.detailTextLabel.text=[NSString stringWithFormat:@"%@" ,[res   objectForKey:@"NoteDate"]];
    return cell;
   }


  @end 

Where is the problem?

UPDATE: okay..I dont know if its the right way or wrong but I called the gets function and then reloaded the tableview inside viewWillAppear method and it worked :)

Upvotes: 0

Views: 83

Answers (3)

JustAnotherCoder
JustAnotherCoder

Reputation: 2575

You have to set the tableview data source and delegate like so. Good place to do this would be in the viewDidLoad method:

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    tabelview.dataSource = self
    tableview.delegate = self

    [self gets];
}

Upvotes: 0

Dan Loughney
Dan Loughney

Reputation: 4677

If you aren't getting any data at all, I'm thinking that you need to set the dataSourceDelegate of your table view. You may be doing that already in IB, but we can't see it in the code.

It also doesn't matter that much, but your tableView outlet can be (weak).

Upvotes: 0

Saheb Roy
Saheb Roy

Reputation: 5957

Simple, After inserting new data in the array just call [tableview reloadData]

Upvotes: 1

Related Questions