Ios 带解析对象的UITableView

Ios 带解析对象的UITableView,ios,uitableview,parse-platform,Ios,Uitableview,Parse Platform,我是新使用解析与iOS。我正在创建一个UITableView,其中应该填充解析对象。方法如下: - (void)viewDidLoad { [super viewDidLoad]; [self performSelector:@selector(retrieveFromParse)]; // Do any additional setup after loading the view from its nib. } - (void) retrieveFromPars

我是新使用解析与iOS。我正在创建一个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显示了两个现有的解析对象

现在是表视图方法:

#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],这不是一个好主意。

方法“findobjectsinbackgroundithblock”是否在单独的线程中运行?
如果是这样,您可能在主线程以外的线程中调用[self.tableView reloadData],这不是一个好主意。

我猜是查询计时与构建视图。而不是

- (void)viewDidLoad {
    [super viewDidLoad];

    // remove this
    [self performSelector:@selector(retrieveFromParse)];
。。。做这个

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self retrieveFromParse];
}

这样,我们就可以确定地知道,在查找完成后,所有视图都已构建并准备就绪。

我的猜测是查询计时与构建视图。而不是

- (void)viewDidLoad {
    [super viewDidLoad];

    // remove this
    [self performSelector:@selector(retrieveFromParse)];
。。。做这个

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self retrieveFromParse];
}
通过这种方式,我们可以确定地知道,所有视图都是在查找完成后构建和准备好的。

请尝试提供常用的Cocoa Touch类,这些类从解析中获取数据

如果最终使用了它,则只需实现以下方法:

  • (PFQuery*)queryForTable
  • (UITableViewCell*)表格视图:cellForRowAtIndexPath:对象:
  • 和其他想要的
    UITableViewDelegate
    方法
当您想要更新tableView时,只需调用
[self-loadObjects]

下面是一个可以工作()的示例类:

#导入
@界面故事板TableViewController:PFQueryTableViewController
@结束
以及实施:

#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
#导入“StoryboardTableViewController.h”
#进口
#进口
#进口
@StoryboardTableViewController的实现
#布拉格标记-
#杂注标记数据
-(PFQuery*)queryForTable{
PFQuery*query=[super queryForTable];
[查询顺序升序:@“优先级”];
返回查询;
}
#布拉格标记-
#pragma标记表视图
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath对象:(PFObject*)对象{
静态NSString*cellIdentifier=@“cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:cellIdentifier];
}
cell.textlab.text=对象[@“title”];
cell.detailTextLabel.text=[NSString stringWithFormat:@“优先级:%@”,对象[@“优先级”];
返回单元;
}
@结束
尝试提供常用的Cocoa Touch类从解析中获取数据的

如果最终使用了它,则只需实现以下方法:

  • (PFQuery*)queryForTable
  • (UITableViewCell*)表格视图:cellForRowAtIndexPath:对象:
  • 和其他想要的
    UITableViewDelegate
    方法
当您想要更新tableView时,只需调用
[self-loadObjects]

下面是一个可以工作()的示例类:

#导入
@界面故事板TableViewController:PFQueryTableViewController
@结束
以及实施:

#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
#导入“StoryboardTableViewController.h”
#进口
#进口
#进口
@StoryboardTableViewController的实现
#布拉格标记-
#杂注标记数据
-(PFQuery*)queryForTable{
PFQuery*query=[super queryForTable];
[查询顺序升序:@“优先级”];
返回查询;
}
#布拉格标记-
#pragma标记表视图
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath对象:(PFObject*)对象{
静态NSString*cellIdentifier=@“cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:cellIdentifier];
}
cell.textlab.text=对象[@“title”];
cell.detailTextLabel.text=[NSString stringWithFormat:@“优先级:%@”,对象[@“优先级”];
返回单元;
}
@结束


您设置了tableView的委托和数据源了吗?@YuviGr,是的,我设置了。我已经用NSArray测试了它,下面显示了数组项。谢谢。colorsArray在哪里申报?@YuviGr,在h申报。代码中一个不寻常的地方是viewDidLoad中的performSelector。您看到的零可能是因为在构建视图时调用了数据源(我认为从iOS8开始)。请添加ViewDidDisplay覆盖,在那里调用super ViewDidDisplay,然后调用(不执行)retrieveFromParse方法。是否设置了tableView的委托和数据源?@YuviGr,是的。我已经用NSArray测试了它,下面显示了数组项。谢谢。colorsArray在哪里申报?@YuviGr,在h申报。代码中一个不寻常的地方是viewDidLoad中的performSelector。您看到的零可能是因为在构建视图时调用了数据源(我认为从iOS8开始)。请添加ViewDidDisplay覆盖,在那里调用super ViewDidDisplay,然后调用(不执行)retrieveFromParse方法。谢谢,明白了,但是我应该在哪里重新加载表视图?我已经在numberOfRowsInSection方法中放置了一个NSLog,该NSLog的输出首先显示,然后是viewDidLoad方法中的NSLog,这应该意味着在从Parse接收数据之前填充表视图,对吗?@mvasco-这个想法很好,但没有用处。块在main上运行,并且更新表的代码已正确放置。您可以尝试以下操作:
[self.tableView performselectornmainthread:@selector(reloa