gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:01:15 24 4
gpt4 key购买 nike

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

但是每次我从列表中选择一个人并发送照片时,它都会得到错误的 user.objectId。它似乎采用另一个名为 Friendship 的类的 Friendship 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

最佳答案

因为 _friends 是一个 Friendship 对象的数组,当点击一行时,你只是直接从数组中取出它,而不是从中获取适当的用户它(就像您在配置单元格标签时所做的那样)。

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

关于ios - 在 didSelectRowAtIndexPath Parse.com 中选择收件人时 PFUser objectId 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30357582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com