1

In the .h file:

@property (weak, nonatomic) IBOutlet UITableView *tableView;

In the .m file:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"RememberedTableListCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.textLabel.text = [[listItems objectAtIndex:indexPath.row] objectForKey:@"word"];
    return cell;
}

- (IBAction)resetList:(UIBarButtonItem *)sender {

    UIAlertView *resetAlert = [[UIAlertView alloc] initWithTitle:@"Reset" message:@"Are you sure to reset the list?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
    [resetAlert show];

}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {

    // the user clicked Yes
    if (buttonIndex == 1) {
        // reset the list

        //reload table data
        NSLog(@"button yes is clicked");

        for (NSMutableDictionary*l in listAll) {
             [l setValue:@"0" forKey:@"remembered"];
        }

        [self.tableView reloadData];
        //[[NSOperationQueue mainQueue]addOperationWithBlock:^{[self.tvRemembered reloadData];}];

        //[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationNone];


        NSLog(@"refreshing...");


    }

}

How to refresh the UITableView with an UIAlertView Delegate? I have tried reloadData: and reloadRowsAtIndexPaths: like the above. But they didn't help. Any help will be appreciated.

tipsywacky
  • 3,374
  • 5
  • 43
  • 75

4 Answers4

2

Some time reloadData not working, so your need to reloadData with some delay of time such like,

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {

    // the user clicked Yes
    if (buttonIndex == 1) {
        [self performSelector:@selector(reloadTableData) withObject:self afterDelay:0.20]; // set afterDelay as per your requirement.
    }
}

And in method

-(void)reloadTableData
{
    [self.tableView reloadData];
}

For more information

https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/reloadData

iPatel
  • 46,010
  • 16
  • 115
  • 137
2

You should just update your data source and calling reloadData should work.

 (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {

    // the user clicked Yes
    if (buttonIndex == 1) 
    {
        // TODO : Update your data source (most likely array) here.   

        NSLog(@"refreshing...");
        [self.tableView reloadData];
    }
}

Hope it helps!

EDIT

I have two comments on your updates:

  1. You are using listItems to populate your cell.textLabel.text and objectForKey word is used. Try to update the value of key word in listItems collection. So the new value is used by your cell event.

  2. Avoid using the same name tableView for your UITableView, as this name is used by its events. I personally either take care to use self methodology or uses a different name to avoid warning of similar instance name.

NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
1

From the Apple Docs, It should not be called in the methods that insert or delete rows, especially within an animation block implemented with calls to beginUpdates and endUpdates

You have to use the reloaddata only, not the beginUpdates

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {

    // the user clicked Yes
    if (buttonIndex == 1) {

        [self.tableView reloadData];

    }

}
Himanshu Joshi
  • 3,391
  • 1
  • 19
  • 32
0

Trying so many things at a time not work Sometimes.

If you have proper connections of delegate & datasource of tableview than just simply : [self.tableView reloadData];

will work.

Mayur Prajapati
  • 5,454
  • 7
  • 41
  • 70