请你看一下这段代码
#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
这个UTTableView
应该转到数据库(这很好),它应该检索前5行,它确实。但是当我
numberofrowsinsection
看不到5的数量?这让我发疯,我做错了什么?
谢谢
答案 0 :(得分:1)
移动以下行:
[self.TableView reloadData];
进入完成区,即:
[query readWithCompletion:^(NSArray *items, NSInteger totalCount, NSError *error)
{
//add the items to our local cop
self.rowitems = [items mutableCopy];
[self.TableView reloadData];
}];
然后原因是:
首先调用numberOfRowsInSection
,然后查询结果。这就是为什么在获取查询结果后重新加载表
答案 1 :(得分:1)
此代码:
[query readWithCompletion:^(NSArray *items, NSInteger totalCount, NSError *error)
{
//add the items to our local cop
self.rowitems = [items mutableCopy];
}];
是异步网络调用(对Azure)。因此,您需要在此调用完成后重新加载表并存储结果。目前,当您重新加载表视图时,self.rowitems
数组为空。
所以,在块内调用reloadData
。
[query readWithCompletion:^(NSArray *items, NSInteger totalCount, NSError *error)
{
//add the items to our local cop
self.rowitems = [items mutableCopy];
[self.TableView reloadData];
}];