App freezes after deleting UITableViewCell - ios

There is a problem when I try to delete UITableViewCell in success block of the request.
In my
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
I am making request with AFNetworking
- (NSURLSessionDataTask *)POST:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
After that in success block I am updating UI by dismissing SVProgressHUD and deleting cell:
NSUserDefaults *usd = [NSUserDefaults standardUserDefaults];
[usd removeObjectForKey:#"payViaCreditCard"];
selectedCardID = nil;
[[CreditCardManager sharedCreditCardManager] removeCreditCard:creditCardToDelete];
[self.tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView reloadData];
In the end my UI not updating and app freezes, when I relaunch the app the data in NSUserDefaults changed.

It would be good if you do either deleteRowsAtIndexPaths:withRowAnimation or tableView reloadData.
[[CreditCardManager sharedCreditCardManager] removeCreditCard:creditCardToDelete];
[self.tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
OR
[[CreditCardManager sharedCreditCardManager] removeCreditCard:creditCardToDelete];
[self.tableView reloadData];

For deleting cell you should use
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSUserDefaults *usd = [NSUserDefaults standardUserDefaults];
[usd removeObjectForKey:#"payViaCreditCard"];
selectedCardID = nil;
[[CreditCardManager sharedCreditCardManager] removeCreditCard:creditCardToDelete];
[self.tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//[self.tableView reloadData];
}
}

Related

iOS/Obj-C - Implement UITableViewCellEditingStyleDelete on one tableview in View Controller?

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;
}
}

Xcode commitEditingStyle UITableViewCellEditingStyleDelete is not working

I have a project I have been working on. The code giving me problems worked yesterday and I haven't made any changes to that part of the code since, when I tried to run it today it wasn't working.
The problem is that when I swipe across the cell to delete it it's not detected and commitEditingStyle method is never run.
I also made another project just to test out the commitEditingStyle method and it was working fine. I also cleaned the project(Product -> Clean) and reset the iOS simulator.
What else can I try?
Here's my code, don't worry about NSUserDefaults, that's for saving the changes after you delete the cell.
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES; }
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self.taskObjects removeObjectAtIndex:indexPath.row];
NSMutableArray *newTaskObjectsData = [[NSMutableArray alloc] init];
for(Model *task in self.taskObjects)
{
[newTaskObjectsData addObject:[self taskObjectAsPropertyList:task]];
}
[[NSUserDefaults standardUserDefaults] setObject:newTaskObjectsData forKey:USER_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
I Hope it's help you
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self.taskObjects removeObjectAtIndex:indexPath.row];
NSMutableArray *newTaskObjectsData = [[NSMutableArray alloc] init];
for(Model *task in self.taskObjects)
{
[newTaskObjectsData addObject:[self taskObjectAsPropertyList:task]];
}
[[NSUserDefaults standardUserDefaults] setObject:newTaskObjectsData forKey:USER_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; // please set automatic animation
}
// here you can reload your tableview
[self.tasksTableView reloadData];
I found out what was wrong! I had another method in the viewController that I was not aware was there.
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
Fixed it by setting the return value to UITableViewCellEditingStyleDelete.

Delete row in UITableView in conjunction with Parse

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];
}];
}
}

After deleting a row from UITableView it is still shown

After deleting a row, it is still showed.
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
Audio *current = [logic findAudioByRow:indexPath.row];
[logic deleteAudio:current callback:^{
dispatch_async(dispatch_get_main_queue(), ^{
//some audio player logic that does not important for the topic
});
}];
}
this method calls that function:
-(void)deleteAudio:(Audio *)audio callback:(voidCallback)callback
{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSString * ownerId = [NSString stringWithFormat:#"%ld", [audio.owner_id integerValue]];
NSString * audioId = [NSString stringWithFormat:#"%ld", [audio.aid integerValue]];
APIData *apiData = [[APIData alloc] initWithMethod:DELETE_AUDIO_BY_ID user:[[UserLogic instance] currentUser] queue:requestQueue params:[[NSMutableDictionary alloc] initWithObjectsAndKeys:audioId,#"aid", ownerId, #"oid", nil]];
[APIRequest executeRequestWithData:apiData block:^(NSURLResponse *response, NSData *data, NSError *error) {
}];
callback();
[self updateContent:YES];
});
}
I don't understand when I should call reloadData to update view.
Can't see the delete method in your code. This is how you have to use the delete row methods.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[mySourceArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
When you delete a row of table view also remove the related record from array, then after reload tableview.

UITableview cell delete. but deosn't reload cell

I want to refresh tagListTableView.
but it doesn't refresh.
When delete button on UITableview cell is tapped , object in coraData's date could be removed.
but, Deleting a cell in a table view is still exposed to the screen to blank.
I tried to [tagListTableView reloadData] on viewWillAppear, viewDidAppear, .....more.
What am I missing??
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
Voca_Tag *tag = (Voca_Tag *)[self.fetchedResultsController objectAtIndexPath:indexPath];
UITableViewCell *cell = [tagListTableView cellForRowAtIndexPath:indexPath];
[cell setSelected:NO animated:YES];
[tag.managedObjectContext deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]]; // [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
[tagListTableView reloadData];
[pickedTags removeObject:tag];
cell.accessoryType = UITableViewCellAccessoryNone;
NSError *error = nil;
if (![tag.managedObjectContext save:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
}
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
if (editingStyle == UITableViewCellEditingStyleDelete) {
Voca_Tag *tag = (Voca_Tag *)[self.fetchedResultsController objectAtIndexPath:indexPath];
UITableViewCell *cell = [tagListTableView cellForRowAtIndexPath:indexPath];
[cell setSelected:NO animated:YES];
[tag.managedObjectContext deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]]; // [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
//********
//REMOVE the object before reloading the table
[pickedTags removeObject:tag];
[tagListTableView reloadData];
cell.accessoryType = UITableViewCellAccessoryNone;
NSError *error = nil;
if (![tag.managedObjectContext save:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
You should remove object from your pickedTags before you reload the tableview, you were doing is after reload.
It might help you.

Resources