简体   繁体   English

在iOS中重新加载UITableView的单元格

[英]Reload the cells of a UITableView in iOS

I have a UITableView in an iOS app that is populated using NSfetchedResultsController. 我在使用NSfetchedResultsController填充的iOS应用程序中有一个UITableView。 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 (eg 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. 目前,显示修改后的单元格的唯一方法是滚动UITableView,以便调用cellForRowAtIndexPath来更新单元格。

Any ideas how to achieve that ? 有什么想法要实现吗? I have tried a couple of approaches but none of them seems to work. 我尝试了几种方法,但似乎都没有用。

Update 1 : 更新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. 我还尝试调用configureCell,这基本上是在我从服务器收到数据包并构建单元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. 更新2:我注意到在表格中加载单元格后,tableView引用变为空。 I have no idea why this happens but it renders reloadData useless. 我不知道为什么会这样,但是它使reloadData变得无用。

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
                 }

             }
         }
     }];

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 : 相应的文档可以在这里找到-http : //developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/reloadRowsAtIndexPaths : withRowAnimation

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

You know point when you'll receive the response from server at that time you just reload the table cell ot tableview. 您知道何时从服务器接收响应时,您只需重新加载tableview表单元。

Check this link 检查此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM