通过提升检索数据和管理来获得双倍数据

时间:2014-05-12 07:24:40

标签: ios parse-platform tableview

我正在从parse.com检索数据 我想按升序设置它们,但我发现一些数据大约是双倍的10倍,而我只想将每个单元格显示为一个用户名数据。

你能看出我做错了吗?

- (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 {
[super viewWillAppear:animated];

PFQuery *query = [PFQuery queryWithClassName:@"Aanvragen"];
[query whereKey:@"RecipientIDs" equalTo:[[PFUser currentUser]objectId]];
[query orderByDescending:@"Datum"];

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

    if (error) {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }

    else {
        // we found messages
        self.messages = objects;
        [self.tableView reloadData];
        NSLog(@"Retrieved %d messages", [self.messages count]);
    }
}];
}

#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.
return [self.messages count];
}

#pragma mark - Table view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


PFObject *message = [self.messages objectAtIndex:indexPath.row];
cell.textLabel.text = [message objectForKey:@"senderName"];

NSString *fileType = [message objectForKey:@"FileType"];

self.objectId = [message objectId];
NSLog(@" objectId: %@", [self objectId]);

if ([fileType isEqualToString:@"image"]) {
    cell.imageView.image = [UIImage imageNamed:@"icon_image"];
}

else if ([fileType isEqualToString:@"video"]) {
    cell.imageView.image = [UIImage imageNamed:@"icon_video"];
}

else {
    cell.imageView.image = nil;
}
return cell;
}

1 个答案:

答案 0 :(得分:0)

您应该更改数据模型。不要使用RecipientID并在其中存储objectId字符串,而是创建一个名为" receiver"这是一个指向User类的指针,然后将用户对象放入其中。这是在类之间创建关系的正确方法:

aanvragen[@"recipient"] = [PFUser currentUser];

现在您可以像这样更改查询约束:

[query whereKey:@"recipient" equalTo:[PFUser currentUser]];

你应该只得到每个对象中的一个。