我正在尝试从朋友页面中删除朋友但没有成功,有人可以帮忙吗?
·H
@property (nonatomic, strong) PFRelation *friendsRelation;
@property (nonatomic, strong) NSArray *friends;
@property (nonatomic, strong) NSMutableArray *unfriend;
@property (nonatomic, strong) PFUser *currentUser;
的.m
self.friendsRelation = [[PFUser currentUser] objectForKey:@"friendsRelation"];
PFQuery *query = [self.friendsRelation query];
[query orderByAscending:@"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){
if (error) {
// NSLog(@"error %@ %@", error, [error userInfo]);
} else {
self.friends = objects;
[self.tableView reloadData];
}
}];
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
PFUser *user = [self.friends objectAtIndex:[self.tableView indexPathForSelectedRow]];
PFRelation *friendsRelation = [self.currentUser relationForKey:@"friendsRelation"];
if( 0 == buttonIndex ){ //cancel button
} else if ( 1 == buttonIndex ){
if ([self isFriend:user]) {
for (PFUser *friend in self.friends) {
if ([friend.objectId isEqualToString:user.objectId]) {
[self.unfriend removeObject:friend];
break;
}
}
[friendsRelation removeObject:user];
} else {
}
[self.currentUser saveInBackgroundWithBlock:^(BOOL succeed, NSError *error) {
if (error) {
NSLog(@"error %@ %@", error, [error userInfo]);
}
}];
}
[self.tableView reloadData];
}
-(BOOL)isFriend:(PFUser *)user
{
for (PFUser *friend in self.friends) {
if ([friend.objectId isEqualToString:user.objectId]) {
return YES;
}
}
return NO;
}
Mutable数组用于添加和删除对象但是目前它是空的我认为这就是为什么它不起作用? 当我按下确定我想从我的朋友关系中删除这位朋友,但此刻一切都没有发生。
答案 0 :(得分:0)
您的代码看起来更新了关系,因此更新了服务器,而不是表视图。
您应该删除unfriend mutable数组,而是从friends数组中删除该朋友。然后重新加载表视图。
答案 1 :(得分:0)
替换 [friendsRelation removeObject:user];
代表
[friendsRelation deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
}