在didSelectRowAtIndexPath Parse.com

时间:2015-05-20 18:25:18

标签: ios objective-c uitableview parse-platform

我目前正在构建一个照片信息应用,用户可以相互发送照片。拍摄照片时,您可以从新的View Controller中的UITableView中选择收件人。

但每次我从列表中选择一个人并发送照片时,都会收到错误的user.objectId。它似乎采取友谊objectId这是另一个名为友谊的类,当它应该采取用户的objectId。以下是我的表现:

@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

1 个答案:

答案 0 :(得分:1)

因为_friendsFriendship个对象的数组,当点击一行时,您只需将其直接从数组中取出,然后再从中获取相应的用户(就像您一样)在配置单元格标签时执行。)

所以在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];
}