索引0超出空数组parse.com的边界

时间:2015-04-11 18:30:52

标签: ios objective-c uitableview nsmutablearray

我正在尝试将当前用户创建的对象列表解析为Parse.com中的自定义表视图。运行时我收到了这个错误:

[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array

以下是我的表现:

@interface ProfileViewController ()

@property (nonatomic, retain) PFQuery *query;

@end

@implementation DailyProfileViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self refresh];

    self.tableView.delegate = self;
    _projects = [[NSMutableArray alloc] initWithCapacity:100];
}

- (void)refresh {

    PFQuery *query = [PFQuery queryWithClassName:@"Projects"];
    [query includeKey:@"user"];
    [query whereKey:@"user" equalTo:[PFUser currentUser]];
    [query orderByDescending:@"createdAt"];

    [query findObjectsInBackgroundWithBlock:^(NSArray *posts, NSError *error) {
        if (!error) {

            [_projects setArray:posts];
            [self.tableView reloadData];
            NSLog(@"%@", posts);
            return;
        }
        NSLog(@"Fetch posts error: %@", error.localizedDescription);
        return;

    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _projects.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 65;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    _project = [_projects objectAtIndex:indexPath.row];

    ProfileProjectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ProfileProjectTableViewCell"];

        if (!_user) _user = [PFUser currentUser];

        cell.item = _project;

        [(PFFile*)_project[@"profilePic"] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
            cell.profilePic.image = [UIImage imageWithData:data];
        }];

        [(PFFile*)_project[@"bgImage"] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
            cell.bgView.image = [UIImage imageWithData:data];
        }];

        cell.projectName.text = _project[@"name"];

        return cell;
}

@end

2 个答案:

答案 0 :(得分:1)

请像这样改变你的viewDidLoad ..

self.tableView.delegate = self;
_projects = [[NSMutableArray alloc]init];
    [self refresh];

你应该在初始化数组后调用refresh方法,并确保你添加的数据不应该为空

答案 1 :(得分:0)

为什么在初始化阵列之前调用- refresh?请改变您的-viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    _projects = [[NSMutableArray alloc] init];
    [self refresh];
}