当我运行以下代码时,UITableView
上没有任何内容。我创建了一个全局NSMutableArray
,用于在Parse上存储查询结果,但我无法使用该数组加载UITableView
上的单元格。
谢谢!
#import "ViewController.h"
#import "MenuViewController.h"
#import "Parse/Parse.h"
@interface ViewController ()
@end
@implementation ViewController {
CLLocationManager *locationManager;
}
@synthesize eventTableView;
@synthesize eventTableViewCell;
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadEvents];
}
- (void) loadEvents
{
eventNames = [[NSMutableArray alloc] init];
PFQuery *event_query = [PFQuery queryWithClassName:@"Event"];
[event_query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(@"Successfully retrieved %lu scores.", (unsigned long)objects.count);
for (PFObject *object in objects) {
[eventNames addObject:[object objectForKey:@"event_name"]];
NSLog(@"%@", [object objectForKey:@"event_name"]);
NSLog(@"%lu", (unsigned long)eventNames.count);
}
} else {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [eventNames count];
}
- (UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
}
NSString *cellText = [NSString stringWithFormat:@"%@",eventNames[indexPath.row]];
NSLog(@"%@", eventNames[indexPath.row]);
cell.textLabel.text = cellText;
return cell;
}
@end
答案 0 :(得分:1)
复制并粘贴以下loadEvents方法
- (void) loadEvents
{
eventNames = [[NSMutableArray alloc] init];
PFQuery *event_query = [PFQuery queryWithClassName:@"Event"];
[event_query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(@"Successfully retrieved %lu scores.", (unsigned long)objects.count);
for (PFObject *object in objects) {
[eventNames addObject:[object objectForKey:@"event_name"]];
NSLog(@"%@", [object objectForKey:@"event_name"]);
NSLog(@"%lu", (unsigned long)eventNames.count);
}
[eventTableView reloadData];
} else {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
每次异步获取表视图的数据时,都必须重新加载整个表或更改的部分。
答案 1 :(得分:0)
我猜测在tableview已经显示之后,findObjectsInBackground的结果返回了。 NSLog是否打印出您期望的结果?
要尝试的一件事是添加
[eventTableView reloadData];
在for循环之后,这将告诉tableview再次重新加载数据。