0

我在使用NSfetchedResultsController填充的iOS应用程序中有一个UITableView。当用户只在没有滚动或与应用程序交互的情况下查看单元格时,可能会有服务器发送影响单个单元格的外部数据包(例如,更改某种状态或标题),在这种情况下,我希望单元格自动更新。此刻,显示修改单元格的唯一方法是滚动UITableView,以便调用cellForRowAtIndexPath来更新单元格。在iOS中重新加载UITableView的单元格

任何想法如何实现?我尝试了一些方法,但没有一个似乎可行。

更新1

我也试着拨打configureCell,基本上改变了电池,只要我收到数据包从服务器更新它的标题,并建立了细胞indexPath。通过使用断点,我发现单元格的标签被改变了,但它并没有反映在屏幕上。

更新2: 我注意到,我的tableView引用在表格中加载单元格后变为null。我不知道为什么发生这种情况,但它使reloadData无用。

+0

[self.tableView reloadRowsAtIndexPaths:@ [indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone]; – Pradeep

+0

我已经试过了,这是很常见的建议。 – user1845360

+0

你使用NSFetchedResultsController委托方法吗?如果不是,为什么不呢?听起来就像你试图自己管理已更改的属性。 –

回答

0

问题解决了,我以编程方式创建了对UIViewController的子类的新引用,并且我失去了对表视图的引用。

-1

你知道什么时候你会收到来自服务器的响应,那时你只是重新加载表格单元格或tableview。

Check this link

+0

在调用reloadRowsAtIndexPaths时不刷新UITableView,也许链接中的人没有使用抓取的控制器......不知道。 – user1845360

0

可能是下面的代码是你

/** 
    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 
       } 

      } 
     } 
    }]; 
0

使用

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

相应的文档可以在这里找到乐于助人 - http://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/reloadRowsAtIndexPaths:withRowAnimation

+0

谢谢你。但是,如果你看到Update 2出于某种原因,当我加载所有数据时,我将失去对tableview的引用,并且当我单击一个UIBarButtonItem时,我似乎将其还原了。我将查看链接以防万一在故事板中设置不正确的东西, – user1845360

相关问题