简体   繁体   English

在didSelectRowAtIndexPath Parse.com中选择收件人时,PFUser objectId错误

[英]Wrong PFUser objectId when selecting recipients in didSelectRowAtIndexPath Parse.com

I'm currently building a photo messaging app where users can send photos to each other. 我目前正在构建一个照片消息传递应用程序,用户可以在其中相互发送照片。 When the photo is taken, you select recipients from a UITableView in a new View Controller. 拍照后,您可以从新视图控制器中的UITableView中选择收件人。

But every time I select a person from the list and send the photo, it gets the wrong user.objectId. 但是每次我从列表中选择一个人并发送照片时,都会得到错误的user.objectId。 It seems to take the Friendship objectId which is another class named Friendship, when it should take the objectId of the user. 似乎应该使用Friendship objectId,这是另一个名为Friendship的类,它应该使用用户的objectId。 Here's how I'm doing it: 这是我的做法:

@implementation PickRecipientsViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.recipients = [[NSMutableArray alloc] init];
    [_selectedImage setImage:_image];
    self.tableView.delegate = self;

    [self refreshFriends];
}

- (void)refreshFriends {

    [__acceptedRequests removeAllObjects];
    PFQuery *friendsQuery = [self queryForFriends];
    PFQuery *acceptedRequestQuery = [self queryForAcceptedFriendRequests];
    PFQuery *friendRequestsQuery = [self queryForRequests];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

        // Find friends
        NSArray *objects = [friendsQuery findObjects];
        for (PFObject * obj in objects) {
            [obj[@"user1"] fetchIfNeeded];
            [obj[@"user2"] fetchIfNeeded];
        }
        _friends = [objects mutableCopy];

        // Find pending requests
        objects = [friendRequestsQuery findObjects];
        for (PFObject *obj in objects) {
            [obj[@"fromUser"] fetchIfNeeded];
        }
        __friendRequests = [objects mutableCopy];

        // Find accepted requests
        objects = [acceptedRequestQuery findObjects];
        for (PFObject *obj in objects) {
            PFUser *to = (PFUser*)[obj[@"toUser"] fetchIfNeeded];
            [obj deleteEventually];
            [__acceptedRequests addObject:to[@"username"]];
        }

        // show accepted requests
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            if (__acceptedRequests.count > 0) {
                NSString *friends = __acceptedRequests[0];
                for (int i = 1; i < __acceptedRequests.count; ++i) {
                    friends = [friends stringByAppendingFormat:@", %@", __acceptedRequests[i]];
                }
                friends = [friends stringByAppendingString:@" accepted your friend request"];

                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New Friends" message:friends delegate:self cancelButtonTitle:@"Wuhu" otherButtonTitles:nil, nil];
                alert.tag = kAlertTagAcceptedRequest;
                [alert show];
            }
        });
    });
}

- (PFQuery *)queryForAcceptedFriendRequests {
    PFUser *user = [PFUser currentUser];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %@ AND (fromUser = %@ AND toUser != %@)", @"approved", user, user];
    PFQuery *acceptedRequestQuery = [PFQuery queryWithClassName:@"FriendRequest" predicate:predicate];
    return acceptedRequestQuery;
}

- (PFQuery *)queryForFriends {
    PFUser *user = [PFUser currentUser];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user1 = %@ AND user2 != %@ OR user1 != %@ AND user2 = %@", user, user, user, user];
    PFQuery *friendsQuery = [PFQuery queryWithClassName:@"Friendship" predicate:predicate];
    return friendsQuery;
}

- (PFQuery *)queryForRequests {
    PFUser *user = [PFUser currentUser];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %@ AND (toUser = %@ AND fromUser != %@)", @"pending", user, user];
    PFQuery *friendRequests = [PFQuery queryWithClassName:@"FriendRequest" predicate:predicate];
    return friendRequests;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RecipientsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RecipientsTableViewCell" forIndexPath:indexPath];

    PFUser *user = [self.friends objectAtIndex:indexPath.row];

    if([self.recipients containsObject:user.objectId]){
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    PFObject *friendRequest = [_friends objectAtIndex:indexPath.row];
    PFUser *user1 = (PFUser *)friendRequest[@"user1"];
    PFUser *user2   = (PFUser *)friendRequest[@"user2"];

    if ([user1.username isEqualToString:[PFUser currentUser].username]) {
        cell.nameL.text = user2[@"username"];

        [(PFFile*)user2[@"profilePic"] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
            if (error) {return;}
            cell.profilePic.image = [UIImage imageWithData:data];
        }];

    } else if ([user2.username isEqualToString:[PFUser currentUser].username]) {
        cell.nameL.text = user1[@"username"];

        [(PFFile*)user1[@"profilePic"] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
            if (error) {return;}
            cell.profilePic.image = [UIImage imageWithData:data];
        }];
    }
    return cell;
}

- (BOOL)isFriend:(PFUser *)user {
    for (PFUser *friend in self.friends) {
        if ([friend.objectId isEqualToString:user.objectId]) {
            return YES;
        }
    }
    return NO;
}

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _friends.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 68;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    PFUser *user = [self.friends objectAtIndex:indexPath.row];

    if (cell.accessoryType == UITableViewCellAccessoryNone){
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.recipients addObject:user.objectId];
    } else{
        cell.accessoryType = UITableViewCellAccessoryNone;
        [self.recipients removeObject:user.objectId];
    }
}

- (IBAction)sendImage {

    PFObject *message = [PFObject objectWithClassName:@"Messages"];

    [message setObject:[PFUser currentUser] forKey:@"fromUser"];
    [message setObject:[PFUser currentUser] forKey:@"toUser"];
    [message setObject:@"image" forKey:@"fileType"];

    [message setObject:self.recipients forKey:@"recipientIds"];
    [message setObject:[[PFUser currentUser] objectId] forKey:@"senderId"];

    // Image
    NSData *imageData = UIImageJPEGRepresentation(_image, 1.0);
    NSString *filename = [NSString stringWithFormat:@"image.png"];
    PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
    [message setObject:imageFile forKey:@"file"];

    [message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {

            // Dismiss the controller
            [[[self presentingViewController] presentingViewController] dismissViewControllerAnimated:YES completion:nil];

        } else {
            [SVProgressHUD showErrorWithStatus:@"Oh darn! Something went wrong :("];
        }
    }];
}

@end

Because _friends is an array of Friendship objects and when a row is tapped you just directly get it out of the array and don't then get the appropriate user from it (like you do when you configure the cell labels). 因为_friendsFriendship对象的数组,所以当点击一行时,您只是直接将其从数组中移出,然后又不从中获取合适的用户(就像配置单元格标签时一样)。

So in tableView:didSelectRowAtIndexPath: you should have something like: 因此,在tableView:didSelectRowAtIndexPath:您应该具有以下内容:

BOOL adding = NO;

if (cell.accessoryType == UITableViewCellAccessoryNone){
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    adding = YES;
} else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

PFObject *friendRequest = [_friends objectAtIndex:indexPath.row];
PFUser *user1 = (PFUser *)friendRequest[@"user1"];
PFUser *user2 = (PFUser *)friendRequest[@"user2"];

PFUser *recipient = nil;

if ([user1.username isEqualToString:[PFUser currentUser].username]) {
    recipient = user2;
} else if ([user2.username isEqualToString:[PFUser currentUser].username]) {
    recipient = user1;
}

if (adding) {
    [self.recipients addObject:recipient.objectId];
} else {
    [self.recipients removeObject:recipient.objectId];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM