0

Can you have a look at this code please

#import "ViewController.h"
#import "DataService.h"


@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITableView *TableView;
@property (strong, nonatomic) DataService *Service;
@property (nonatomic, strong)   MSTable *table;
//@property (nonatomic, strong)   MSClient *client;
@property (nonatomic, strong)   NSMutableArray *items;

@end

@implementation ViewController

@synthesize Service;
@synthesize rowitems;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.client = [MSClient clientWithApplicationURLString:@"https://outnight-mobile.azure-mobile.net/"
                                            applicationKey:@"okYeRGfBagYrsbkaqWIRObeDtktjkF10"];
    self.table = [self.client tableWithName:@"notifications"];
    self.rowitems = [[NSMutableArray alloc] init];
    MSQuery *query = [self.table query];
    query.fetchLimit = 5;
    [query readWithCompletion:^(NSArray *items, NSInteger totalCount, NSError *error)
     {
         //add the items to our local cop
         self.rowitems = [items mutableCopy];


     }];
    [self.TableView reloadData];



}


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

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
    //return 5;
     NSLog(@"%d",[self.rowitems count]);
    return 5;


}



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

        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        cell.textLabel.text = @"fool";
        return cell;
}


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

@end

This UTTableView should go to the database (which is does fine) and it should retrieve back the first 5 rows and it does. but when I

numberofrowsinsection does not see the amount of 5 when i do a array count ?? This is driving me crazy, what am I doing wrong ?
thanks

1
  • What is show in the cells? Commented Feb 6, 2014 at 11:47

2 Answers 2

1

This code:

[query readWithCompletion:^(NSArray *items, NSInteger totalCount, NSError *error)
 {
     //add the items to our local cop
     self.rowitems = [items mutableCopy];
 }];

is an asynchronous network call (to Azure). So, you need to reload the table after this call has completed and you have stored the results. Currently when you reload the table view the self.rowitems array is empty.

So, call reloadData inside the block.

[query readWithCompletion:^(NSArray *items, NSInteger totalCount, NSError *error)
 {
     //add the items to our local cop
     self.rowitems = [items mutableCopy];

     [self.TableView reloadData];
 }];
Sign up to request clarification or add additional context in comments.

1 Comment

Was going to say the same thing.
1

Move following line:

[self.TableView reloadData];

into completion block i.e., :

[query readWithCompletion:^(NSArray *items, NSInteger totalCount, NSError *error)
     {
         //add the items to our local cop
         self.rowitems = [items mutableCopy];

         [self.TableView reloadData];
 }];

Then reason is: numberOfRowsInSection gets called first and result of query comes later. that's why reload table after query result has been fetched

1 Comment

Brilliant guys - you've saved the day! it does work after all .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.