Skip navigation

tableview reloaddata - changes are not shown until display is touched

4708 Views 7 Replies Latest reply: Feb 4, 2011 2:33 PM by nasholic RSS
nasholic Calculating status...
Currently Being Moderated
Feb 1, 2011 4:31 PM
hello

I have a problem with the reloaddata method of uitableview. my tableview shows the values of a nsmanagedobject from my context. the object is updated from time to time and therefor I added an observer in my uitableview class how checks for changes and calls the reloaddata method if changes are made. now a strange behavior occurs since the device *does not show the updated values until I touch/scroll the display*. I googled a lot but I did not find a solution for this. any suggestions how this behavior occurs and can be corrected? thanks for your time. the code is the following..


#import "UAVDetailViewController.h"
#import "UAV.h"
#import "MapViewController.h"
#import "RightNavViewController.h"

@implementation UAVDetailViewController

@synthesize uavObject, tableDetailContents;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
[super viewDidLoad];

//Get managedObjectContext from MapView
MapViewController *mapViewController;
if (UIUSER_INTERFACEIDIOM() == UIUserInterfaceIdiomPad) {
RightNavViewController *right = [[[self.splitViewController.viewControllers objectAtIndex:1] viewControllers] objectAtIndex:0];
mapViewController = right.mapViewController;

} else {
mapViewController = (MapViewController *)[[self.tabBarController viewControllers] objectAtIndex:0];
}

NSManagedObjectContext *context = mapViewController.managedObjectContext;

//register observer for context in notification center
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateTable:)
name:NSManagedObjectContextObjectsDidChangeNotification
object:context];


self.contentSizeForViewInPopover = CGSizeMake(320.0, 460.0);
self.navigationItem.title = [uavObject valueForKey:@"hostName"];

//for cells
self.tableDetailContents = [[NSArray alloc] initWithObjects:@"Hostname", @"IP Address", @"Status", @"Scenario", @"Longitude", @"Latitude", @"Height", @"Battery", @"Heading", @"Speed", nil];

}



- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}

//get uav from the context, assuming that the hostname won't change during flight
- (void)updateTable:(NSNotification *)theNotification {
NSManagedObjectContext *context = [theNotification object];
//check all updated objects if they are the uav object of this view controller
for(NSManagedObject *object in [[theNotification userInfo] objectForKey:NSUpdatedObjectsKey]) {
if ([object isMemberOfClass:[UAV class]]) {

NSManagedObject *toCheck = [context objectWithID:[object objectID]];

if ([[toCheck valueForKey:@"hostName"] isEqualToString:[self.uavObject valueForKey:@"hostName"]]) {
self.uavObject = toCheck;
[[self tableView] reloadData];
}
}
}
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [self.tableDetailContents count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"UAVDetailCell";
NSUInteger row = [indexPath row];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [self.tableDetailContents objectAtIndex:row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;

switch (row) {
case 0: //hostname
cell.detailTextLabel.text = [self.uavObject valueForKey:@"hostName"];
break;
case 1: //ip
cell.detailTextLabel.text = [self.uavObject valueForKey:@"ipAddress"];
break;
case 2: //status
if ([[self.uavObject valueForKey:@"state"] intValue] == 0) {
cell.detailTextLabel.text = @"UAVSTATEUNINITIALIZED";
} else if ([[self.uavObject valueForKey:@"state"] intValue] == 1) {
cell.detailTextLabel.text = @"UAVSTATEUNCONFIGURED";
} else if ([[self.uavObject valueForKey:@"state"] intValue] == 2) {
cell.detailTextLabel.text = @"UAVSTATE_AIRBORNE_AWAITINGPOS1";
} else if ([[self.uavObject valueForKey:@"state"] intValue] == 3) {
cell.detailTextLabel.text = @"UAVSTATE_AIRBORNE_AWAITING_FLIGHTDIRECTION";
} else if ([[self.uavObject valueForKey:@"state"] intValue] == 4) {
cell.detailTextLabel.text = @"UAVSTATE_AIRBORNE_AWAITINGPOS2";
} else if ([[self.uavObject valueForKey:@"state"] intValue] == 5) {
cell.detailTextLabel.text = @"UAVSTATE_AIRBORNE_REACHED_MAXDISTANCE";
} else if ([[self.uavObject valueForKey:@"state"] intValue] == 6) {
cell.detailTextLabel.text = @"UAVSTATE_AIRBORNE_FLYINGCENTER";
} else if ([[self.uavObject valueForKey:@"state"] intValue] == 7) {
cell.detailTextLabel.text = @"UAVSTATE_AIRBORNE_REACHEDCENTER";
} else if ([[self.uavObject valueForKey:@"state"] intValue] == 8) {
cell.detailTextLabel.text = @"UAVSTATE_FLYINGHOME";
} else if ([[self.uavObject valueForKey:@"state"] intValue] == 9) {
cell.detailTextLabel.text = @"UAVSTATEHOME";
} else if ([[self.uavObject valueForKey:@"state"] intValue] == 10) {
cell.detailTextLabel.text = @"UAVSTATEABORTED";
}

break;
case 3: //scenario
if ([[self.uavObject valueForKey:@"scenario"] intValue] == 0) {
cell.detailTextLabel.text = @"UAVSCENARIONONE";
} else if ([[self.uavObject valueForKey:@"scenario"] intValue] == 1) {
cell.detailTextLabel.text = @"UAVSCENARIOAIRBORNE";
} else if ([[self.uavObject valueForKey:@"scenario"] intValue] == 2) {
cell.detailTextLabel.text = @"UAVSCENARIOMULTIHOP";
} else if ([[self.uavObject valueForKey:@"scenario"] intValue] == 3) {
cell.detailTextLabel.text = @"UAVSCENARICOVERAGE";
}
break;
case 4: //longitude
cell.detailTextLabel.text = @"to do";
break;
case 5: //latitude
cell.detailTextLabel.text = @"to do";
break;
case 6: //height
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", [[self.uavObject valueForKey:@"height"] intValue]];
break;
case 7: //battery
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", [[self.uavObject valueForKey:@"battery"] intValue]];
break;
case 8: //heading
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", [[self.uavObject valueForKey:@"heading"] intValue]];
break;
case 9: //speed
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", [[self.uavObject valueForKey:@"speed"] intValue]];
break;

default:
break;
}

return cell;
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}


- (void)dealloc {
[tableDetailContents release];
[uavObject release];

[super dealloc];
}


@end
iOS 4, iPhone / iPad
  • RayNewbie Level 5 Level 5 (6,810 points)
    Hi Nasholic -
    nasholic wrote:
    the object is updated from time to time and therefor I added an observer in my uitableview class how checks for changes and calls the reloaddata method if changes are made. now a strange behavior occurs since the device *does not show the updated values until I touch/scroll the display*.


    //get uav from the context, assuming that the hostname won't change during flight
    - (void)updateTable:(NSNotification *)theNotification {
    NSManagedObjectContext *context = [theNotification object];
    NSLog(@"%s: context=%@", _func_, context);

    //check all updated objects if they are the uav object of this view controller
    for(NSManagedObject *object in [[theNotification userInfo] objectForKey:NSUpdatedObjectsKey]) {
    if ([object isMemberOfClass:[UAV class]]) {

    NSManagedObject *toCheck = [context objectWithID:[object objectID]];

    NSLog(@"--> toCheck=%@ toCheck.hostName=%@", toCheck, [toCheck valueForKey:@"hostName"]);
    NSLog(@"--> self.uavObject=%@ self.uavObject.hostName=%@", self.uavObject, [self.uavObject valueForKey:@"hostName"]);

    if ([[toCheck valueForKey:@"hostName"] isEqualToString:[self.uavObject valueForKey:@"hostName"]]) {
    NSLog(@"--> Should Reload Data Now <--");

    self.uavObject = toCheck;
    [[self tableView] reloadData];
    }
    }
    }
    }

    Since reloadData is only called in one place, I think that's where we need to look. I would add some logging to make sure reloadData is being sent when you expect. If that's not helpful, please post the log produced by running the above. - Ray
    iMac, Mac OS X (10.5.8)
  • xnav Level 5 Level 5 (6,615 points)
    I don't see where

    - (void)updateTable:(NSNotification *)theNotification {

    is updating tableDetailContents, the array which actually backs the tableview?
    Mac Pro 2x3GHz Dual, Mac OS X (10.6.6), BlueTooth Kybd, Trackpad, BootCamp WinXP, iPhone, iPad
  • xnav Level 5 Level 5 (6,615 points)
    Maybe you have to refetch as in this [thread|http://discussions.apple.com/thread.jspa?threadID=2733014]?
    Mac Pro 2x3GHz Dual, Mac OS X (10.6.6), BlueTooth Kybd, Trackpad, BootCamp WinXP, iPhone, iPad
  • xnav Level 5 Level 5 (6,615 points)

    if ([[toCheck valueForKey:@"hostName"] isEqualToString:[self.uavObject valueForKey:@"hostName"]]) {
    self.uavObject = toCheck;
    [[self tableView] reloadData];

    Check with a breakpoint if [self tableView] is returning nil.
    Mac Pro 2x3GHz Dual, Mac OS X (10.6.6), BlueTooth Kybd, Trackpad, BootCamp WinXP, iPhone, iPad

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.