Iphone 在iOS中重新加载UITableView的单元格
我在使用NSfetchedResultsController填充的iOS应用程序中有一个UITableView。当用户只是查看单元格而没有滚动或与应用程序交互时,服务器可能会发送影响单个单元格的外部数据包(例如,更改某种状态或标题),在这种情况下,我希望自动更新单元格。目前,显示修改后的单元格的唯一方法是滚动UITableView,以便调用CellForRowatineXpath来更新单元格 有什么办法可以做到这一点吗?我试过几种方法,但似乎都不管用 更新1: 我还尝试调用configureCell,它基本上会在我从服务器接收到数据包并构建单元格时修改单元格以更新其标题。通过使用断点,我看到单元格的标签发生了变化,但它没有反映在屏幕上 更新2:Iphone 在iOS中重新加载UITableView的单元格,iphone,ios,objective-c,core-data,nsfetchedresultscontroller,Iphone,Ios,Objective C,Core Data,Nsfetchedresultscontroller,我在使用NSfetchedResultsController填充的iOS应用程序中有一个UITableView。当用户只是查看单元格而没有滚动或与应用程序交互时,服务器可能会发送影响单个单元格的外部数据包(例如,更改某种状态或标题),在这种情况下,我希望自动更新单元格。目前,显示修改后的单元格的唯一方法是滚动UITableView,以便调用CellForRowatineXpath来更新单元格 有什么办法可以做到这一点吗?我试过几种方法,但似乎都不管用 更新1: 我还尝试调用configureCe
我注意到在表格中加载单元格后,我的tableView引用变为null。我不知道为什么会发生这种情况,但它会使重新加载的数据变得无用。您知道何时会收到服务器的响应,此时您只需重新加载tableview中的表格单元格
可能在下面,代码对您很有帮助
/**
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
}
}
}
}];
/**
HTTP常量
**/
#定义超时时间间隔60
#定义内容类型@“内容类型”
#定义URL_ENCODED@“应用程序/x-www-form-urlencoded”
#定义GET@“GET”
#定义POST@“POST”
-(NSMutableURLRequest*)getNSMutableURLRequestUsingPOSTMethodWithUrl:(NSString*)url postData:(NSString*)postData
{
NSMutableURLRequest*req=[NSMutableUrlRequestWithURL:[NSURL URLWithString:url]缓存策略:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:TIMEOUT_INTERVAL];
[req setHTTPMethod:POST];
[req addValue:URL_编码的HttpHeaderField:CONTENT_类型];
[req setHTTPBody:[\u postData数据使用编码:NSUTF8StringEncoding];
返回请求;
}
NSString*_postData={}
NSMutableURLRequest*req=[self-getNSMutableURLRequestUsingPOSTMethodWithUrl:LOGIN_-URL-postData:_-postData];
[NSURLConnection sendAsynchronousRequest:req队列:[NSOperationQueue mainQueue]completionHandler:^(NSURResponse*响应,NSData*数据,NSError*错误)
{
if(error)//表示服务器错误
{
//连接服务器时出错
}
其他的
{
n错误*errorInjson解析;
NSDictionary*json=[NSJSONSerialization JSONObjectWithData:数据选项:NSJSONReadingMutableContainers错误:&errorInJsonParsing];
if(errorInJsonParsing)//json中的错误解析
{
//json解析中出错
}
其他的
{
@试一试
{
NSLog(@“json==%@”,json);
[self.tableView重新加载rowsatindexpaths:@[indexPathOfYourCell]和rowAnimation:UITableViewRowAnimationNone];
}
@捕获(NSException*异常)
{
//例外情况
}
}
}
}];
使用
[UITableView重载rowsatindexpaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:yourRowNumber Instition:yourSectionNumber]]withRowAnimation:UITableViewRowAnimationFade]代码>
可以在此处找到相应的文档-:问题已解决,我以编程方式创建了对UIViewController子类的新引用,但丢失了对表视图的引用。[self.tableView reloadrowsatinexpaths:@[indexPathOfYourCell]with RowAnimation:UITableViewRowAnimationNone];我已经尝试过了。这是一个非常常见的建议。您是否使用NSFetchedResultsController委托方法?若否,原因为何?听起来像是您自己尝试管理更改的属性。您指的是哪种方法?主要是控制器:didChangeObject:atIndexPath:forChangeType:newindexath:
,但它们都是获得有效属性所必需的。UITableView在调用ReloadRowsatindExpath时不会刷新,也许链接中的人没有使用抓取的控制器…不知道。谢谢。但是,如果您看到更新2,由于某种原因,当加载所有数据时,我丢失了对tableview的引用,而当我单击UIBarButtonim时,我似乎又恢复了它。。。如果我在故事板中设置不正确,我将查看链接,