Frage

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

War es hilfreich?

Lösung

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

Andere Tipps

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top