gpt4 book ai didi

ios - 数组计数的 UITableView 问题

转载 作者:行者123 更新时间:2023-11-28 22:16:12 24 4
gpt4 key购买 nike

你能看看这段代码吗

#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 的数量??这让我发疯,我做错了什么?
谢谢

最佳答案

这段代码:

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

是一个异步网络调用(到 Azure)。因此,您需要在调用完成并存储结果后重新加载表。当前,当您重新加载 TableView 时,self.rowitems 数组为空。

因此,在 block 内调用 reloadData

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

[self.TableView reloadData];
}];

关于ios - 数组计数的 UITableView 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21602016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com