gpt4 book ai didi

ios - UITableView不会显示数据

转载 作者:行者123 更新时间:2023-11-29 02:17:55 24 4
gpt4 key购买 nike

我有一个 UITableView,它拒绝显示任何数据,Xcode 没有显示任何错误或警告。不知道为什么这是因为相同的代码在不同的应用程序中工作(显然数组和 TableView 的名称不同)。我将委托(delegate)和数据源都设置为 View View Controller 。如果有什么我错过的请告诉我!这是我的代码:

#import "ViewController.h"

@interface UIViewController ()

@end

@implementation ViewController
@synthesize CafeTableView;

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self performSelector:@selector(RetriveData)];
[CafeTableView reloadData];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)RetriveData {
PFQuery *query = [PFQuery queryWithClassName:@"CafeList"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(@"%@",objects);
NSLog(@"Successfully retrieved %d Cafes.", objects.count);
cafeListArry = [[NSArray alloc] initWithArray:objects];

}else{
UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:@"Error"
message:(@"There has been an error loading the Cafe List Please Try Again!")
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[theAlert show];
NSLog(@"%@",error);
}
}];
[CafeTableView reloadData];
}

//-------------------TABLE VIEW-----------------------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return cafeListArry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CafeCell";
CafeListCell *cell = [CafeTableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...

PFObject *tempObject = [cafeListArry objectAtIndex:indexPath.row];

cell.title.text = [tempObject objectForKey:@"CafeName"];
cell.detail.text = [tempObject objectForKey:@"NumberOfStars"];

return cell;
}


@end

最佳答案

按照其他人的建议,将您的调用移至 block 内的 reloadData 并按如下方式更改 if 条件:

- (void)RetriveData {
PFQuery *query = [PFQuery queryWithClassName:@"CafeList"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (objects) {
NSLog(@"%@",objects);
NSLog(@"Successfully retrieved %d Cafes.", objects.count);
cafeListArry = [[NSArray alloc] initWithArray:objects];
[CafeTableView reloadData]; // HERE
}else{
UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:@"Error"
message:(@"There has been an error loading the Cafe List Please Try Again!")
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[theAlert show];
NSLog(@"%@",error);
}
}];
}

此外,按照 Lyndsey 在评论中的建议,更改以下行

cell.detail.text = [tempObject objectForKey:@"NumberOfStars"];

cell.detail.text = [[tempObject objectForKey:@"NumberOfStars"] stringValue];

因为你说是数字,不是字符串。

关于ios - UITableView不会显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28533422/

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