Search code examples
iphoneobjective-cuitableviewviewwillappear

Delete Rows from UITableView


Here is the code for my delete, it works fine but you have to open another tab and then click this tab again for the item to be removed. As this is not the normal UITableView you cannot just remove from array and then update table. so could someone please help me refresh the view. Also the snippet below does not work:

// Reload the view completely
if ([self isViewLoaded]) {
    self.view=nil;
    [self viewDidLoad];
}

Here is my delete code:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete){
        // Delete song from list & disc \\

        //Remove from arrays
        [self.Writer removeObjectAtIndex:[indexPath row]];
        [self.Book removeObjectAtIndex:[indexPath row]];
        [self.BookImageId removeObjectAtIndex:[indexPath row]];

        NSString *TempWriter = [self.ArtistNamesArray componentsJoinedByString:@","];
        NSString *TempBook = [self.SongNamesArray componentsJoinedByString:@","];
        NSString *TempBookImageId = [self.YoutubeIDSArray componentsJoinedByString:@","];

        TempWriter = [TempWriter substringToIndex:[TempArtistNames length] - 1];
        TempBook = [TempBook substringToIndex:[TempSongNames length] - 1];
        TempBookImageId = [TempBookImageId substringToIndex:[TempYouTube length] - 1];


        //Save Details
        [[NSUserDefaults standardUserDefaults] setValue:[NSString stringWithFormat:@"%@,", TempBook] forKey:@"BookName"];
        [[NSUserDefaults standardUserDefaults] setValue:[NSString stringWithFormat:@"%@,", TempWriter] forKey:@"WriterName"];
        [[NSUserDefaults standardUserDefaults] setValue:[NSString stringWithFormat:@"%@,", TempBookImageId] forKey:@"ImageId"];
        [[NSUserDefaults standardUserDefaults] synchronize];

    }


}

and my ViewWillAppear code is:

- (void) viewWillAppear: (BOOL) animated
{
    // Reload the view completely
    if ([self isViewLoaded]) {
        self.view=nil;
        [self viewDidLoad];
    }

}

Thanks for all who will help


Solution

  • You don't appear to be calling the UITableView reloadData method at any point after you've carried out the deletion.

    As such, adding...

    [tableView reloadData];
    

    ...to the bottom of your - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath method should solve the problem.