簡體   English   中英

在iOS中重新加載UITableView的單元格

[英]Reload the cells of a UITableView in iOS

我在使用NSfetchedResultsController填充的iOS應用程序中有一個UITableView。 當用戶只是在不滾動或不與應用程序交互的情況下查看單元格時,服務器可能會發送外部數據包,從而影響單個單元格(例如,更改某種狀態或標題),在這種情況下,我需要該單元格自動更新。 目前,顯示修改后的單元格的唯一方法是滾動UITableView,以便調用cellForRowAtIndexPath來更新單元格。

有什么想法要實現嗎? 我嘗試了幾種方法,但似乎都沒有用。

更新1

我還嘗試調用configureCell,這基本上是在我從服務器收到數據包並構建單元indexPath時,修改該單元以更新其標題。 通過使用斷點,我看到單元格的標簽已更改,但未反映在屏幕上。

更新2:我注意到在表格中加載單元格后,tableView引用變為空。 我不知道為什么會這樣,但是它使reloadData變得無用。

可能在下面的代碼對您有幫助

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

             }
         }
     }];

采用

[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

問題解決了,我以編程方式創建了對UIViewController子類的新引用,而我丟失了對表格視圖的引用。

您知道何時從服務器接收響應時,您只需重新加載tableview表單元。

檢查此鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM