【问题标题】:Reload the cells of a UITableView in iOS在 iOS 中重新加载 UITableView 的单元格
【发布时间】:2013-07-08 14:14:43
【问题描述】:

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

任何想法如何实现?我尝试了几种方法,但似乎都没有。

更新 1

我还尝试调用 configureCell,它基本上会修改单元格以更新其标题,只要我从服务器收到数据包并构建单元格 indexPath。通过使用断点,我看到单元格的标签已更改,但未反映在屏幕中。

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

【问题讨论】:

  • [self.tableView reloadRowsAtIndexPaths:@[indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
  • 我试过了..这是很常见的建议。
  • 你使用 NSFetchedResultsController 委托方法吗?如果不是,为什么不呢?听起来您尝试自己管理更改的属性。
  • 你是指哪些方法?
  • 主要是controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:,但它们都是获得工作NSFetchedResultsControllerDelegate所必需的。

标签: iphone ios objective-c core-data nsfetchedresultscontroller


【解决方案1】:

下面的代码可能对你有帮助

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

             }
         }
     }];

【讨论】:

    【解决方案2】:

    使用

    [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:

    【讨论】:

    • 谢谢。但是,如果您出于某种原因看到更新 2,则在加载所有数据时我会丢失对 tableview 的引用,并且当我单击 UIBarButtonItem 时似乎会恢复它...我会查看链接以防万一在情节提要中设置错误,
    【解决方案3】:

    问题已解决,我正在以编程方式创建对 UIViewController 子类的新引用,但我丢失了对表格视图的引用。

    【讨论】:

      【解决方案4】:

      您知道何时会收到来自服务器的响应,此时您只需重新加载表格单元格和表格视图。

      Check this link

      【讨论】:

      • UITableView 在调用 reloadRowsAtIndexPaths 时没有刷新,可能链接中的人没有使用获取的控制器......不知道。
      猜你喜欢
      • 1970-01-01
      • 2021-03-21
      • 1970-01-01
      • 2011-12-23
      • 2011-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多