Monday, 9 September 2013

Refreshing the UITableView only works when I move the table?

Refreshing the UITableView only works when I move the table?

I have a UITableView where a user clicks on an accessory. The accessory
launches a UIView in modal view (using presentViewController).
When it returns it is meant to update the tableview cell with the required
data.
Currently I am using a Singleton to capture userData inside a dictionary;
but even if I use a NSNotification I have the same issue.
My issue is that when the modal dialog is dismissed the tableview never
updates; however if I scroll or otherwise physically move then table the
entry in the cell is updated.
I get the same results if I try to force reload a cell or the entire
tableview with NSNotifications.
I am wondering if I can get some help on this; how do I make the cell reload?
The code relating to cell display and how the modal is presented is below;
Notes;
_eventName is just a NSString
-(void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ( [[MySingleton sharedInstance].userData objectForKey:@"eventName"]
!=nil) {
_eventName = [[MySingleton sharedInstance].userData
objectForKey:@"eventName"];
}
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
NSDictionary *dict;
NSString *name;
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
dict = [_template objectAtIndex:indexPath.row];
name = [dict objectForKey:@"name"];
cell.textLabel.text = name;
NSString *key = [[dict objectForKey:@"key"] lowercaseString];
if ([[key lowercaseString] isEqualToString:@"name"]==YES) {
cell.detailTextLabel.text = _eventName;
}
return cell;
}
-(void) tableView:(UITableView *)tv
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dict = [_template objectAtIndex:indexPath.row];
NSString *key = [[dict objectForKey:@"key"] lowercaseString];
NSString *name = @"";
if ([key isEqualToString:@"name"]==YES) {
EventNameVC *nameEditor = [[EventNameVC alloc]
initWithNibName:@"EventNameVC" bundle:nil];
if (_eventName !=nil) {
nameEditor.txtName.text = _eventName;
} else {
nameEditor.txtName.text = name;
}
UINavigationController *nav = [[UINavigationController alloc]
initWithRootViewController:nameEditor];
// When we return from the view, reload the cell.
[self presentViewController:nav animated:YES completion:^{
[self.tv reloadRowsAtIndexPaths:[NSArray
arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationRight];
}];
nav = nil;
}
}

No comments:

Post a Comment