I have three tableviews inside of one View Controller (their visibility is controlled by a segment control). That said, I only want cells to have the option of being deleted from self.friendsView, and not the other tableviews. I have the following code below in my View Controller, but the ability to swipe and delete a cell is visible on all three of my tableviews, not just self.friendsView. How can I fix this?
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.friendsView) {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
NSMutableDictionary *nodeData = [[self.myFriendData objectAtIndex:indexPath.row] mutableCopy];
NSString *nid = [nodeData objectForKey:#"nid"];
[nodeData setObject:nid forKey:#"nid"];
NSLog(#"%#",nid);
[self.myFriendData removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[DIOSNode nodeDelete:nodeData success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"node deleted!");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"could not delete node!");
}];
}
}
}
In addition to implementing commitEditingStyle you also need to implement the editingStyleForRowAtIndexPath delegate method.
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == self.friendsView) {
return UITableViewCellEditingStyleDelete;
} else {
return UITableViewCellEditingStyleNone;
}
}
Related
I'm implementing "swipe to delete" to a TableView with notifications from API. I created a method that deletes a notification when I hard-code its notification id (which is an array). The problem is I can't figure out how to get the exact notification id to delete.
There are TableView Delegate and TableView Data Source methods that somehow get the notification id, so I suppose I should be able to get it for the purpose of my method, but I've run out of ideas.
Here's my API source code:
desc 'delete notifications'
params do
requires :notification_ids, type: Array
end
delete 'notifications/remove', root: :notifications, each_serializer: NotificationSerializer do
require_authentication!
NotificationLogic.delete_notifications params[:notification_ids], current_user
current_user.notifications
end
Here's the method for deleting notifications:
-(void)deleteNotificationWithId:(NSArray*)ids withCompletionHandler:(DeleteNotificationCompletionHandler)handler
{
NSDictionary* params = #{ #"notification_ids" : ids };
__weak typeof(self) weakSelf = self;
ReadNotificationRequest* req = [ReadNotificationRequest new];
req.notificationIds = ids;
[_objectManager deleteObject:nil
path:#"user/notifications/remove"
parameters:params
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
_secondTry = NO;
NSArray* arr = mappingResult.array;
[self notififyAboutNotifications:arr];
handler(YES, arr, nil);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
if (operation.HTTPRequestOperation.response.statusCode == 401 && !_secondTry)
{
[weakSelf relogin:^{
[weakSelf deleteNotificationWithId:ids withCompletionHandler:handler];
}];
return;
}
handler(NO, nil, error);
}];
}
and implementation of the method in NotificationTableView. It works, but I hard-code the array with number:
-(void)setNotifications:(NSMutableArray *)notifications{
_notifications = notifications;
[self reloadData];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//Remove item in array
[self.notifications removeObjectAtIndex:indexPath.row];
// Also remove that row from the table view with an animation
[tableView deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
//Remove hard-coded notification from server
[[Api sharedInstance]deleteNotificationWithId:#[#756]
withCompletionHandler:^(BOOL succes, Message *response, NSError *error) {
if(succes){
} else {
[Utils alert:error.pop_message];
}
}];}
}
#pragma mark TableView Data Source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.notifications.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NotificationTableViewCell* cell = [self dequeueReusableCellWithIdentifier:#"NotificationTableViewCell"];
[cell configureCellWithNotification:self.notifications[indexPath.row]];
return cell;
}
#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Notification* not = self.notifications[indexPath.row];
[self.notificationDelegate notificationTapped:not];
}
This code
//Remove item in array
[self.notifications removeObjectAtIndex:indexPath.row];
Deletes the information you need, just before you need it. Instead of deleting it, read the ID out first, then delete it and use the ID.
Have friendsviewcontroller in which have uibarbuttonItem to edit friends list and other uibarbuttonitem to create groups for group chatrooms.
Have multiple segue for switching view controllers.
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Groups";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"New" style:UIBarButtonItemStylePlain target:self
action:#selector(actionNew)];
self.tableView.separatorInset = UIEdgeInsetsZero;
chatrooms = [[NSMutableArray alloc] init];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if ([PFUser currentUser] != nil)
{
[self refreshTable];
}
else LoginUser(self);
}
#pragma mark - User actions
- (void)actionNew
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Create New Group" message:nil delegate:self
cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex=
{
if (buttonIndex != alertView.cancelButtonIndex)
{
UITextField *textField = [alertView textFieldAtIndex:0];
if ([textField.text isEqualToString:#""] == NO)
{
PFObject *object = [PFObject objectWithClassName:PF_CHATROOMS_CLASS_NAME];
object[PF_CHATROOMS_NAME] = textField.text;
[object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
{
if (error == nil)
{
[self refreshTable];
}
else [ProgressHUD showError:#"Network error."];
}];
}
}
}
- (void)refreshTable
{
[ProgressHUD show:nil];
PFQuery *query = [PFQuery queryWithClassName:PF_CHATROOMS_CLASS_NAME];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (error == nil)
{
[chatrooms removeAllObjects];
for (PFObject *object in objects)
{
[chatrooms addObject:object];
}
[ProgressHUD dismiss];
[self.tableView reloadData];
}
else [ProgressHUD showError:#"Network error."];
}];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [chatrooms count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
PFObject *chatroom = chatrooms[indexPath.row];
cell.textLabel.text = chatroom[PF_CHATROOMS_NAME];
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
PFObject *chatroom = [chatrooms objectAtIndex:indexPath.row];
[chatrooms removeObjectAtIndex:chatroom];
//[chatrooms removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
PFObject *chatroom = chatrooms[indexPath.row];
NSString *roomId = chatroom.objectId;
CreateMessageItem([PFUser currentUser], roomId, chatroom[PF_CHATROOMS_NAME]);
ChatView *chatView = [[ChatView alloc] initWith:roomId];
chatView.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:chatView animated:YES];
}
Deleted row in table view reappears when navigate back to the TableView
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
PFObject *chatroom = [chatrooms objectAtIndex:indexPath.row];
[chatrooms removeObjectAtIndex:chatroom];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
Im unable to locate what piece of code i m missing or what exactly i m doing wrong.
If anyone can please point out to that.
Will appreciate so much.
Thanks.
When committing deletion, your code removes objects from chatrooms which is data source of the table view in your case, but this happens in your app's memory, the source from which chatrooms is populated does not change. Thus, speaking in MVC, the app's model state is not updated after the view's state is changed.
Every time the table view is showed up, your code populates chatrooms in refreshTable, if the model's state hasn't been changed, the code gets same list as before, thus the table view doesn't change.
EDIT: Instead of using another approach to refresh the table view, you need to think about what does your app do in this table view. If user can delete stuff in the table view, should your app update model (This model can be a local or remote database, a property list file, etc.) too? If yes, then update model when user inserts or deletes rows in the table view; well, if not, then you are asking a question that is not a problem, or maybe the table view should turn off editing.
EDIT1:
If you do need to update data, based on your code, you may need to do something like this:
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
PFObject *chatroom = [chatrooms objectAtIndex:indexPath.row];
[chatrooms removeObjectAtIndex:indexPath.row];
PFQuery *query = [PFQuery queryWithClassName:PF_CHATROOMS_CLASS_NAME];
[query deleteChatroom:chatroom];
[tableView deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
That is, you may need to implement method deleteChatroom: of class PFQuery.
I am trying to delete a row in a UITableView (PFQueryTableViewController). The object deletes in the class, but is only reflected in the table when I refresh the table. Here is the code I am using.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (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) {
[tableView reloadData];
}];
}
}
Sussed it.
Instead of [tableView reloadData], I've used [self loadObjects].
However, the usual delete animation is not there.
You have to make all UI updates in main thread.
- (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) {
dispatch_async(dispatch_get_main_queue(), ^{
[tableView deleteRowsAtIndexPaths:#[indexPath]withRowAnimation:UITableViewRowAnimationFade];
}];
}
}
Any insight would be greatly appreciated.
I have an app with a shared photo stream and I need the user to only be able to swipe and delete their own photo items...my basic code setup works to delete with swipe, but I can't figure out the best way to code so that it verifies first that the PFUser matches as the owner of the PFOject before allowing for swipe and delete
- (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,
As per your code you have to manually refresh the table because you have not refreshed the table. Add the code as follows
- (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];
}];
}
This worked for me
- (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];
}
}];
}
}
First off ,in your photo class you need to have a pointer to the user who owns the photo.
Now in your table view controller you have an array of your PFObjects that you called objects.
So to allow the user to delete only his/her photos you could do something like this.
- (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;
}
However a better way to do this is to have a custom class and make your table view controller model that class as the following:
#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
In my table view controller I would have something like this
#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
The project I'm working on required me to use a sqlite database, and I've been trying to get swipe to delete to work on my tableView:
[self performSelectorOnMainThread:#selector(removeMovieFromCache:) withObject:[NSNumber numberWithInt:movieId] waitUntilDone:YES];
[db performSelectorOnMainThread:#selector(performQuery:) withObject:[NSString stringWithFormat:#"DELETE FROM movie WHERE id = %d", movieId] waitUntilDone:YES];
[db performSelectorOnMainThread:#selector(performQuery:) withObject:[NSString stringWithFormat:#"DELETE FROM new_movies WHERE new_movie_id = %d", movieId] waitUntilDone:YES];
[self removeMovieFromCache:movieId];
[db performQueryWithFormat:#"DELETE FROM movie WHERE id = %d", movieId];
[db performQueryWithFormat:#"DELETE FROM new_movies WHERE new_movie_id = %d", movieId];
[db performQuery:#"COMMIT"];
That's the code to get rid of something from my database. When I try to apply this to my swipe to delete command of:
-
(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)movieID
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
//code goes here
}
}
It just doesn't want to work, what am I doing wrong?
In your tableView Data Source, try implementing this:
(UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath
{
return UITableViewCellEditingStyleDelete;
}
(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)movieID
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// First delete the row from the table, then delete from the DB, finally reload the date in the table
[theTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
// code to delete from DB
[theTable reloadData];
}
}
(replace "theTable" with whatever you've called your table!)