문제

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

도움이 되었습니까?

해결책

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top