Delete Rows from UITableView

user393273 picture user393273 · Mar 27, 2011 · Viewed 11.6k times · Source

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

Answer

unexpectedvalue picture unexpectedvalue · Mar 27, 2011

Well, you either need to call reloadData or – deleteRowsAtIndexPaths:withRowAnimation:.