无法获取子类UITableViewCells以显示成功查询的Parse对象的数据

时间:2014-05-31 00:40:10

标签: ios objective-c cocoa-touch ios7 parse-platform

这个问题已成功回答;谢谢jsksma2。

我无法获取我的数据来填充TableView中的行,即使我正确地恢复了数据并且可以对tableview进行硬编码以显示静态数量的虚拟文本。我有一个预感,我的问题与initWithStyle和initWithCoder有关,用于子类UITableViewCells。

在UITableViewController的一个子类中,名为" GiveItemsTableViewC",在viewDidLoad期间,我正在查询Parse中每个被称为" PFGiveItem"的对象。我得到了这些并将每一个添加到一个全局变量,一个名为" myGiveItems"的可变数组。我记录这些,我得到了我正在寻找的东西,所以这部分正在发挥作用。

GiveItemsTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    PFQuery *query = [PFQuery queryWithClassName:@"giveItem"];
    [query whereKey:@"giver" equalTo:[PFUser currentUser]];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            self.myGiveItems = [[NSMutableArray alloc]init];
            for (PFObject *object in objects) {
                PFGiveItem *newGiveItem = [[PFGiveItem alloc]init];
                newGiveItem.giveItemName = object[@"giveItemTitle"];
                newGiveItem.giveItemImage = object[@"giveItemPhoto"];
                [self.myGiveItems addObject:newGiveItem];
            }
        } else {
            // Log details of the failure
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }

    }];
}

现在我尝试将这些giveItems中的每一个加载到TableView对象中,使用自定义的TableViewCells,每个都调用" GiveItemCell。"

GiveItemCell.m

@implementation JFGiveItemCell



@synthesize giveItemImageView = _giveItemImageView;
@synthesize giveItemLabel = _giveItemLabel;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

回到表视图控制器中,我返回一个用于表视图的部分。 当我为rowsInSection包含一个静态数字时,我可以将测试值输出到每个单元格。如果我执行下面的代码,我会得到一个tableView,其中包含标签为" Test",根据即将推出的cellForRowAtIndexPath方法。因此它适用于该测试,但显然我希望动态加载适当的信息。

GiveItemsTableViewController

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}


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

    static NSString *cellIdentifier = @"Cell";

    JFGiveItemCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil){
        cell = [[JFGiveItemCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    //    PFGiveItem *giveItem = self.myGiveItems[indexPath.row];
    //    cell.giveItemLabel.text = giveItem.giveItemName;
    cell.giveItemLabel.text = @"Test";
    return cell;
}

2 个答案:

答案 0 :(得分:0)

有几个问题......

您的numberOfRowsInSection应该返回myGiveItems数组的大小。

当您异步加载项目时,需要告诉表视图重新加载。

您不需要实现部分数量,默认为1。

答案 1 :(得分:0)

您似乎忘记在阻止方法的回调中调用[tableView reloadData]

- (void)viewDidLoad
{
    [super viewDidLoad];
    PFQuery *query = [PFQuery queryWithClassName:@"giveItem"];
    [query whereKey:@"giver" equalTo:[PFUser currentUser]];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            self.myGiveItems = [[NSMutableArray alloc]init];
            for (PFObject *object in objects) {
                PFGiveItem *newGiveItem = [[PFGiveItem alloc]init];
                newGiveItem.giveItemName = object[@"giveItemTitle"];
                newGiveItem.giveItemImage = object[@"giveItemPhoto"];
                [self.myGiveItems addObject:newGiveItem];
            }
            [self.tableView reloadData];
        } else {
            // Log details of the failure
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }

    }];
}

另外,我第二个@CrimsonChris说你需要正确设置你的dataSource方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return self.myGiveItems.count
}