为什么我的tableView是空白的?我没有正确地重新加载我的数据源吗?

时间:2013-03-23 16:44:05

标签: iphone ios objective-c

viewWillAppearnumberOfRowsInSection中的两个NSLog都显示了我想要在tableView中填充的数据。

我认为这个问题与返回正确的计数有关但是,由于NSLogs匹配,我不确定那是不是问题。

另外,我认为这是问题,因为cellForRowAtIndexPath没有被调用。

我没有正确地重新加载我的tableView吗?非常感谢帮忙。

#import "MainQueryViewController.h"
#import <Parse/Parse.h>
#import "SVProgressHUD.h"
#import "HHPanningTableViewCell.h"
#import "HHInnerShadowView.h"
#import "HHDirectionPanGestureRecognizer.h"

@interface MainQueryViewController () <PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate, HHPanningTableViewCellDelegate>


@end

@implementation MainQueryViewController

@synthesize listArray;



- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {

        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    PFLogInViewController *login = [[PFLogInViewController alloc] init];
    login.fields = PFLogInFieldsFacebook;
    login.delegate = self;
    login.signUpController.delegate = self;
    [self presentModalViewController:login animated:NO];



}

-(void)viewWillAppear:(BOOL)animated {
    PFQuery *query = [PFQuery queryWithClassName:@"ListItem"];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            [query whereKey:@"listName" equalTo:[PFUser currentUser]];
            listArray = [objects mutableCopy];
            NSLog(@"I'm about to show you an array");
            NSLog(@"%@", listArray);
        }
        else {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];

}


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

#pragma mark - Table view data source

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



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    HHPanningTableViewCell *cell = (HHPanningTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSInteger directionMask = indexPath.row % 5;
    if (cell == nil) {
        NSLog(@"Cell = nil.");
        cell = [[HHPanningTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];




    UIView *drawerView = [[UIView alloc] initWithFrame:cell.frame];

    drawerView.backgroundColor = [UIColor greenColor];
    cell.textLabel.text =  [listArray objectAtIndex:indexPath.row];
    cell.drawerView = drawerView;


    // Configure the cell...

}
    if (directionMask < 3) {
        cell.directionMask = directionMask;
    }
    else {
        cell.directionMask = HHPanningTableViewCellDirectionLeft + HHPanningTableViewCellDirectionRight;

        if (directionMask == 4) {
            cell.delegate = self;
        }
    }

    cell.textLabel.text = [self.listArray objectAtIndex:directionMask];

    return cell;
}



#pragma mark - PFLogInViewController delegate

- (void)logInViewController:(PFLogInViewController *)logInController didLogInUser:(PFUser *)user
{
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"Successfully logged in.");
        [SVProgressHUD showSuccessWithStatus:@"Success!"];




    }];

}

- (void)logInViewControllerDidCancelLogIn:(PFLogInViewController *)logInController
{
    [self dismissModalViewControllerAnimated:YES];
    NSLog(@"Login was cancelled!");
}

- (void)signUpViewController:(PFSignUpViewController *)signUpController didSignUpUser:(PFUser *)user
{
    [self dismissModalViewControllerAnimated:YES];
    NSLog(@"Successfully signed up.");
}

- (void)signUpViewControllerDidCancelSignUp:(PFSignUpViewController *)signUpController
{
    [self dismissModalViewControllerAnimated:YES];
    NSLog(@"Sign up was cancelled!");
}



#pragma mark - Table view delegate

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if ([cell isKindOfClass:[HHPanningTableViewCell class]]) {
        HHPanningTableViewCell *panningTableViewCell = (HHPanningTableViewCell*)cell;

        if ([panningTableViewCell isDrawerRevealed]) {
            return nil;
        }
    }

    return indexPath;
}

- (void)panningTableViewCellDidTrigger:(HHPanningTableViewCell *)cell inDirection:(HHPanningTableViewCellDirection)direction
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Custom Action"
                                                    message:@"You triggered a custom action"
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}



#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    PFQuery *query = [PFQuery queryWithClassName:@"ListItem"];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            [query whereKey:@"listName" equalTo:[PFUser currentUser]];
            listArray = [objects mutableCopy];
            NSLog(@"I'm about to show you an array");
            NSLog(@"%@", listArray);
        }
        else {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];

    NSLog(@"This is a count of listArray");
    NSLog(@"%@", listArray);
    return [listArray count];
}


@end

enter image description here

2 个答案:

答案 0 :(得分:3)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section末尾的数量是多少?

因为如果我查看PFQuery的文档,我必须说我从未使用过。

  

<强> findObjectsInBackgroundWithBlock:
  异步查找对象并使用结果调用给定的块    - (void)findObjectsInBackgroundWithBlock:(PFArrayResultBlock)块   参数
  块
  要执行的块。该块应具有以下参数签名:(NSArray对象,NSError错误)
  讨论
  异步查找对象并使用结果调用给定的块。

描述表明,在方法结束时,Array的计数为“0”。只有在您运行该方法的block运行后,您才会拥有一个包含数据的数组。此时你需要保存数组并调用[tableView reloadData]

这可以解释这一点:

  

另外,我认为那是问题,因为没有调用cellForRowAtIndexPath。

这可能是因为部分行为0的结果,因为如果你的numberOfRowsInSection  调用方法意味着您的dataSource已正确设置。

但您的代码还有一个更大的问题。
因为每次表视图都想重新加载它的数据时,你会发出一个获取请求,它会要求表视图重新加载自身,这反过来会要求一个新的查询,它将要求tableView重新加载自己,这反过来又会......你明白了。

您应该在viewDidLoad,viewWillAppear或viewController生命周期中对该查询有意义的其他位置进行查询,具体取决于数据的易变性,需要调用的频率等等

还要记住,更新tableView必须在主线程上完成。 PFQuery的文档没有指定块运行时你将在哪个线程。

答案 1 :(得分:-1)

获取数据后重新加载表视图: 添加以下行:

listArray = [objects mutableCopy];
[tableView reloadData]