我有一个UITableView,它拒绝显示任何数据,Xcode没有显示任何错误或警告。不知道为什么这是因为相同的代码在不同的应用程序中工作(显然数组和tableview的名称不同)。我将委托和数据源都设置为视图视图控制器。如果有什么我错过了请告诉我! 这是我的代码:
#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
答案 0 :(得分:1)
按照其他人的建议将您的电话转移到该区域内的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];
因为你说它是一个数字,而不是一个字符串。
答案 1 :(得分:0)
请检查代表是否已连接xib文件,如果没有,请正确连接,请拨打[tableview reloadData];您希望使用表
绑定数据时的方法