带有 Parse 对象的 UITableView
UITableView with Parse objects
我是 iOS 使用 Parse 的新手。我正在创建一个应该用 Parse 对象填充的 UITableView。这是方法:
- (void)viewDidLoad {
[super viewDidLoad];
[self performSelector:@selector(retrieveFromParse)];
// Do any additional setup after loading the view from its nib.
}
- (void) retrieveFromParse {
PFQuery *retrieveColors = [PFQuery queryWithClassName:@"cadenas"];
[retrieveColors findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
colorsArray = [[NSArray alloc] initWithArray:objects];
NSLog(@"descargado=%@",colorsArray);
}
[self.tableView reloadData];
}];
}
前面的 NSLog 显示了两个现有的 Parse 对象。
现在 table 查看方法:
#pragma mark - Table view data source
- (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.
// Usually the number of items in your array (the one that holds your list)
return [colorsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Where we configure the cell in each row
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
PFObject *tempObject = [colorsArray objectAtIndex:indexPath.row];
NSLog(@"CADNA =%@:",[colorsArray objectAtIndex:indexPath.row]);
cell.textLabel.text = [tempObject objectForKey:@"chain_name"];;
return cell;
}
最后一个NSLog没有被调用,所以UITableView是空的
我做错了什么?
谢谢。
方法"findObjectsInBackgroundWithBlock" 运行是否在单独的线程中?
如果是这样,您可能会在主线程以外的线程中调用 [self.tableView reloadData],这并不是一个好主意。
我的猜测是查询时间与构建视图。而不是...
- (void)viewDidLoad {
[super viewDidLoad];
// remove this
[self performSelector:@selector(retrieveFromParse)];
...这样做...
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self retrieveFromParse];
}
通过这种方式,我们可以确定地知道在查找完成后所有视图都已构建并准备就绪。
试试 ParseUI repository,它提供了常用的 Cocoa Touch class 从解析中获取数据。
如果你最终要使用它,你需要实现的只是这些方法:
(PFQuery *)queryForTable
(UITableViewCell *)tableView: cellForRowAtIndexPath: object:
- 和其他想要的
UITableViewDelegate
方法
当你想更新tableView时,只需调用[self loadObjects];
这里有一个示例 class 可以工作 (taken from the ParseUI repo demo):
#import <ParseUI/ParseUI.h>
@interface StoryboardTableViewController : PFQueryTableViewController
@end
和实施:
#import "StoryboardTableViewController.h"
#import <Parse/PFObject.h>
#import <Parse/PFQuery.h>
#import <ParseUI/PFTableViewCell.h>
@implementation StoryboardTableViewController
#pragma mark -
#pragma mark Data
- (PFQuery *)queryForTable {
PFQuery *query = [super queryForTable];
[query orderByAscending:@"priority"];
return query;
}
#pragma mark -
#pragma mark TableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = object[@"title"];
cell.detailTextLabel.text = [NSString stringWithFormat:@"Priority: %@", object[@"priority"]];
return cell;
}
@end
我是 iOS 使用 Parse 的新手。我正在创建一个应该用 Parse 对象填充的 UITableView。这是方法:
- (void)viewDidLoad {
[super viewDidLoad];
[self performSelector:@selector(retrieveFromParse)];
// Do any additional setup after loading the view from its nib.
}
- (void) retrieveFromParse {
PFQuery *retrieveColors = [PFQuery queryWithClassName:@"cadenas"];
[retrieveColors findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
colorsArray = [[NSArray alloc] initWithArray:objects];
NSLog(@"descargado=%@",colorsArray);
}
[self.tableView reloadData];
}];
}
前面的 NSLog 显示了两个现有的 Parse 对象。
现在 table 查看方法:
#pragma mark - Table view data source
- (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.
// Usually the number of items in your array (the one that holds your list)
return [colorsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Where we configure the cell in each row
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
PFObject *tempObject = [colorsArray objectAtIndex:indexPath.row];
NSLog(@"CADNA =%@:",[colorsArray objectAtIndex:indexPath.row]);
cell.textLabel.text = [tempObject objectForKey:@"chain_name"];;
return cell;
}
最后一个NSLog没有被调用,所以UITableView是空的
我做错了什么?
谢谢。
方法"findObjectsInBackgroundWithBlock" 运行是否在单独的线程中? 如果是这样,您可能会在主线程以外的线程中调用 [self.tableView reloadData],这并不是一个好主意。
我的猜测是查询时间与构建视图。而不是...
- (void)viewDidLoad {
[super viewDidLoad];
// remove this
[self performSelector:@selector(retrieveFromParse)];
...这样做...
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self retrieveFromParse];
}
通过这种方式,我们可以确定地知道在查找完成后所有视图都已构建并准备就绪。
试试 ParseUI repository,它提供了常用的 Cocoa Touch class 从解析中获取数据。
如果你最终要使用它,你需要实现的只是这些方法:
(PFQuery *)queryForTable
(UITableViewCell *)tableView: cellForRowAtIndexPath: object:
- 和其他想要的
UITableViewDelegate
方法
当你想更新tableView时,只需调用[self loadObjects];
这里有一个示例 class 可以工作 (taken from the ParseUI repo demo):
#import <ParseUI/ParseUI.h>
@interface StoryboardTableViewController : PFQueryTableViewController
@end
和实施:
#import "StoryboardTableViewController.h"
#import <Parse/PFObject.h>
#import <Parse/PFQuery.h>
#import <ParseUI/PFTableViewCell.h>
@implementation StoryboardTableViewController
#pragma mark -
#pragma mark Data
- (PFQuery *)queryForTable {
PFQuery *query = [super queryForTable];
[query orderByAscending:@"priority"];
return query;
}
#pragma mark -
#pragma mark TableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = object[@"title"];
cell.detailTextLabel.text = [NSString stringWithFormat:@"Priority: %@", object[@"priority"]];
return cell;
}
@end