0

I have a UITableView in an iOS app that is populated using NSfetchedResultsController. When the user is just looking at the cells without scrolling or interacting with the app, there might be an external packet send by a server which affects a single cell (e.g. change some kind of status or title), in this case I want the cell to be updated automatically. At the moment the only way to display the modified cell is by scrolling the UITableView so that cellForRowAtIndexPath is called to update the cell.

Any ideas how to achieve that ? I have tried a couple of approaches but none of them seems to work.

Update 1:

I also tried to call configureCell, which basically modifies the cell to update its title, as soon as I receive the packet from the server and build the cell indexPath. By using breakpoints I see that the label of the cell is changed but it is not reflected in the screen.

Update 2: I have noticed that my tableView reference becomes null after the cells are loaded in the table. I have no idea why this happens but it renders reloadData useless.

user1845360
  • 847
  • 2
  • 12
  • 29

4 Answers4

0

may be below code is helpfull for you

/**
    HTTP CONSTANTS
 **/
#define TIMEOUT_INTERVAL 60
#define CONTENT_TYPE @"Content-Type"
#define URL_ENCODED @"application/x-www-form-urlencoded"
#define GET @"GET"
#define POST @"POST"

-(NSMutableURLRequest*)getNSMutableURLRequestUsingPOSTMethodWithUrl:(NSString *)url postData:(NSString*)_postData
{
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:TIMEOUT_INTERVAL];
    [req setHTTPMethod:POST];
    [req addValue:URL_ENCODED forHTTPHeaderField:CONTENT_TYPE];
    [req setHTTPBody: [_postData dataUsingEncoding:NSUTF8StringEncoding]];
    return req;
}



   NSString *_postData = {<Your postData>}
    NSMutableURLRequest *req = [self getNSMutableURLRequestUsingPOSTMethodWithUrl:LOGIN_URL postData:_postData];
    [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         if (error)// it means server error
         {
             //error while connecting server
         }
         else
         {
             NSError *errorInJsonParsing;
             NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&errorInJsonParsing];
             if(errorInJsonParsing) //error parsing in json
             {
                 //error in json parsing
             }
             else
             {
                 @try
                 {
                    NSLog(@"json==%@",json);
                   [self.tableView reloadRowsAtIndexPaths:@[indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
                 }
                 @catch (NSException *exception)
                 {
                     //exception
                 }

             }
         }
     }];
Pradeep
  • 1,043
  • 7
  • 12
0

Use

[UITableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:yourRowNumber inSection:yourSectionNumber]] withRowAnimation:UITableViewRowAnimationFade];

Corresponding documentation can be found here - http://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/reloadRowsAtIndexPaths:withRowAnimation:

Sid
  • 9,508
  • 5
  • 39
  • 60
  • Thanks for that. However, if you see the Update 2 for some reason I lose the reference to the tableview when all data are loaded and I seem to get it back when I click a UIBarButtonItem... I will have a look at the link in case I have set something up incorrectly in the storyboard, – user1845360 Jul 10 '13 at 08:19
0

Problem solved, I was creating programmatically a new reference to the subclass of the UIViewController and I was losing the reference to the table view.

user1845360
  • 847
  • 2
  • 12
  • 29
-1

You know point when you'll receive the response from server at that time you just reload the table cell ot tableview.

Check this link

Community
  • 1
  • 1
nik
  • 2,289
  • 6
  • 37
  • 60
  • The UITableView is not refreshed upon calling reloadRowsAtIndexPaths, maybe the guy in the link is not using a fetched controller...no idea. – user1845360 Jul 08 '13 at 14:26