如何将刷卡编码为删除,以允许用户仅在iOS中删除其解析数据
如有任何见解,将不胜感激 我有一个共享照片流的应用程序,我需要用户只能滑动和删除他们自己的照片项目…我的基本代码设置可以使用滑动进行删除,但我无法找出最佳的编码方式,以便在允许滑动和删除之前,先验证PFUser是否与PFOject的所有者匹配如何将刷卡编码为删除,以允许用户仅在iOS中删除其解析数据,ios,parse-platform,Ios,Parse Platform,如有任何见解,将不胜感激 我有一个共享照片流的应用程序,我需要用户只能滑动和删除他们自己的照片项目…我的基本代码设置可以使用滑动进行删除,但我无法找出最佳的编码方式,以便在允许滑动和删除之前,先验证PFUser是否与PFOject的所有者匹配 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return YES; } - (void)tab
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ PFObject *object = [self.objects objectAtIndex:indexPath.row];
[object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
}];
}
Grazie,根据您的代码,您必须手动刷新表,因为您尚未刷新表。添加如下代码
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Remove the row from data model
PFObject *object = [self.objects objectAtIndex:indexPath.row];
[object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
// Add refresh control & reload the tableView.
[self refreshControl];
[tableView reloadData];
}];
}
这对我有用
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
PFObject *object = [self.objects objectAtIndex:indexPath.row];
[object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
[self loadObjects];
}
}];
}
}
首先,在photo类中,您需要有一个指向照片所有者的指针 现在,在表视图控制器中,您有一个称为对象的PFObjects数组 因此,要允许用户只删除他/她的照片,您可以这样做
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
PFObject* photoObject=self.objects[indexPath.row];
PFUser* owner=photoObject[@"owner"];
[owner fetchIfNeeded];
if ([owner.objectId isEqualToString:[PFUser currentUser].objectId]) {
return YES;
}
else return NO;
}
但是,更好的方法是拥有一个自定义类,并使您的表视图控制器按如下方式对该类进行建模:
@interface MyPhoto : NSObject <NSCoding>
@property UIImage* photo;
@property PFObject* parseObject;
@property BOOL iOwnThisPhoto;
//Class method to fetch the photo stream from parse and return an array of MyPhoto*
+(NSArray*)fetchStream;
@end
谢谢你的意见。我找到了正确的方法,它似乎工作了-(BOOL)tableView:(UITableView*)tableView caneditrowatinexpath:(nsindepath*)indepath:(PFUser*)user{if(![user.objectId isEqualToString:[PFUser currentUser].objectId]{return YES;}return NO;}
@interface PhotoStreamVC ()
//Private property
@property NSMutableArray* photos;
@end
@implementation PhotoStreamVC
- (void)viewDidLoad
{
[super viewDidLoad];
//Do background fetch
[self performSelectorInBackground:@selector(fetchPhotoStream) withObject:nil];
}
-(void)fetchPhotoStream
{
self.photos=[MyPhoto fetchStream];
//Reload table view from the main thread
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
MyPhoto* photo=self.photoStream[indexPath.row];
return photo.iOwnThisPhoto;
}
@end