无法在UICollectionView中显示解析数据

时间:2015-07-11 16:53:48

标签: ios parse-platform

我是iOS编程的新手,我试图在集合视图中显示Parse数据。我试图在密钥中显示字符串到集合视图单元格的标签。我抓住了#34;文件夹"在viewWillAppear:但似乎无法将其显示在标签中。我的代码如下。

@interface FoldersViewController ()

@end

@implementation FoldersViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    PFUser *currentUser = [PFUser currentUser];
    if (currentUser) {
        NSLog(@"current user %@", currentUser.username);
    } else {
        [self performSegueWithIdentifier:@"showLogin" sender:self];
    }
}


- (void)viewWillAppear:(BOOL)animated {
    PFQuery *query = [PFQuery queryWithClassName:@"Folders"];
    [query whereKeyExists:@"folderName"];
    [query orderByAscending:@"folderName"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error) {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        } else {
            // We found folders
            self.folders = objects;
            [self.collectionView reloadData];
            NSLog(@"Retrieved %d folders", (int)[self.folders count]);
        }
    }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.folders count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = @"Cell";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    PFObject *folder = [self.folders objectAtIndex:indexPath.row];

    UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
    titleLabel.text = (NSString*)self.folders;

    [collectionView reloadData];
    return cell;
}

2 个答案:

答案 0 :(得分:0)

您应该将标签的文本设置为文件夹(以及您要显示的文件夹的任何属性),而不是数组:

titleLabel.text = folder

此外,没有必要在cellForItemAtIndexPath中调用reloadData。

答案 1 :(得分:0)

我想出了如何通过将我的单元格标签与IBAction Collection而不是常规IBAction连接来显示单元格。我试图建立一个记笔记应用程序,唯一的问题是每个单元格只是说"标签"而不是Parse上保存的文件夹名称。不知道我在这里做错了什么:

@interface FoldersViewController ()

@end

@implementation FoldersViewController

- (void)viewDidLoad {
[super viewDidLoad];

PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
    NSLog(@"current user %@", currentUser.username);
} else {
    [self performSegueWithIdentifier:@"showLogin" sender:self];
}
}


-(void)viewWillAppear:(BOOL)animated {
PFQuery *query = [PFQuery queryWithClassName:@"Folders"];
[query whereKeyExists:@"folderName"];
[query orderByAscending:@"folderName"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){
    if (error) {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    } else {
        //We found folders
        self.folders = objects;
        [self.collectionView reloadData];
        NSLog(@"Retrieved %d folders", (int)[self.folders count]);
    }
}];
}



- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.folders count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

FolderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

UILabel *folder = (UILabel *)self.folders;

cell.folderTitle = (NSArray *)folder;


return cell;

}