Eam*_*orr 18 iphone objective-c uitableview
我似乎无法找到关于这个问题的任何信息,所以我想我会问社区.
基本上,我有一个UITableView,我想在我的服务器加载数据时显示一个活动指示器.
这是我正在尝试做的一些示例代码(我正在使用ASIHttpRequest).
//self.listData = [[NSArray alloc] initWithObjects:@"Red", @"Green", @"Blue", @"Indigo", @"Violet", nil]; //this works
NSString *urlStr=[[NSString alloc] initWithFormat:@"http://www.google.com"]; //some slow request
NSURL *url=[NSURL URLWithString:urlStr];
__block ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setCompletionBlock:^{
self.listData = [[NSArray alloc] initWithObjects:@"Red", @"Green", @"Blue", @"Indigo", @"Violet", nil]; //this doesn't work...
[table reloadData];
}];
[request setFailedBlock:^{
}];
[request startAsynchronous];
Run Code Online (Sandbox Code Playgroud)
对google.com的虚拟请求什么都不做 - 它只是创建了一个延迟,在响应中我希望用我自己网站上的一些JSON响应来重新填充表格.
但是当我尝试用颜色填充表格时,没有任何反应!我只是得到一张空白的表...如果我取消注释上面的行,它工作正常,它只是在http响应事情不适合我.
任何建议都非常感谢.
我做了一个[self.tableView reloadData];,现在它有效......
Mar*_*rra 64
NSURLConnection不难使用,会产生更好,更高性能的代码.UITableView.我再次推荐Core Data.我建议回顾一下MVC是如何工作的,你是设计的短路,这是核心问题.
这里有一个更详细的说明.首先,您希望数据检索是异步的.最简单,最可重用的方法是构建一个简单的NSOperation子类.
@class CIMGFSimpleDownloadOperation;
@protocol CIMGFSimpleDownloadDelegate <NSObject>
- (void)operation:(CIMGFSimpleDownloadOperation*)operation didCompleteWithData:(NSData*)data;
- (void)operation:(CIMGFSimpleDownloadOperation*)operation didFailWithError:(NSError*)error;
@end
@interface CIMGFSimpleDownloadOperation : NSOperation
@property (nonatomic, assign) NSInteger statusCode;
- (id)initWithURLRequest:(NSURLRequest*)request andDelegate:(id<CIMGFSimpleDownloadDelegate>)delegate;
@end
Run Code Online (Sandbox Code Playgroud)
此子类是从URL下载内容的最基本方法.用a NSURLRequest和delegate 构造它.它将回调成功或失败.实施只是稍长一点.
#import "CIMGFSimpleDownloadOperation.h"
@interface CIMGFSimpleDownloadOperation()
@property (nonatomic, retain) NSURLRequest *request;
@property (nonatomic, retain) NSMutableData *data;
@property (nonatomic, assign) id<CIMGFSimpleDownloadDelegate> delegate;
@end
@implementation CIMGFSimpleDownloadOperation
- (id)initWithURLRequest:(NSURLRequest*)request andDelegate:(id<CIMGFSimpleDownloadDelegate>)delegate
{
if (!(self = [super init])) return nil;
[self setDelegate:delegate];
[self setRequest:request];
return self;
}
- (void)dealloc
{
[self setDelegate:nil];
[self setRequest:nil];
[self setData:nil];
[super dealloc];
}
- (void)main
{
[NSURLConnection connectionWithRequest:[self request] delegate:self];
CFRunLoopRun();
}
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSHTTPURLResponse*)resp
{
[self setStatusCode:[resp statusCode]];
[self setData:[NSMutableData data]];
}
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)newData
{
[[self data] appendData:newData];
}
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
[[self delegate] operation:self didCompleteWithData:[self data]];
CFRunLoopStop(CFRunLoopGetCurrent());
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
[[self delegate] operation:self didFailWithError:error];
CFRunLoopStop(CFRunLoopGetCurrent());
}
@synthesize delegate;
@synthesize request;
@synthesize data;
@synthesize statusCode;
@end
Run Code Online (Sandbox Code Playgroud)
现在这个课非常可重复使用.您可以根据需要添加NSURLConnection的其他委托方法.NSURLConnection可以处理重定向,身份验证等.我强烈建议您查看其文档.
从这里,您可以CIMGFSimpleDownloadOperation从您UITableViewController的应用程序的另一部分中分离出来.对于这个演示,我们将在UITableViewController.根据您的应用需求,您可以在任何有意义的地方启动数据下载.对于此示例,我们将在视图出现时将其踢开.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSURLRequest *request = ...;
CIMGFSimpleDownloadOperation *op = [[CIMGFSimpleDownloadOperation alloc] initWithURLRequest:request andDelegate:self];
[[NSOperationQueue mainQueue] addOperation:op];
[self setDownloadOperation:op]; //Hold onto a reference in case we want to cancel it
[op release], op = nil;
}
Run Code Online (Sandbox Code Playgroud)
现在,当视图出现时,将执行异步调用并下载URL的内容.在此代码中将通过或失败.失败第一:
- (void)operation:(CIMGFSimpleDownloadOperation*)operation didFailWithError:(NSError*)error;
{
[self setDownloadOperation:nil];
NSLog(@"Failure to download: %@\n%@", [error localizedDescription], [error userInfo]);
}
Run Code Online (Sandbox Code Playgroud)
成功之后,我们需要解析返回的数据.
- (void)operation:(CIMGFSimpleDownloadOperation*)operation didCompleteWithData:(NSData*)data;
{
[self setDownloadOperation:nil];
NSLog(@"Download complete");
//1. Massage the data into whatever we want, Core Data, an array, whatever
//2. Update the UITableViewDataSource with the new data
//Note: We MIGHT be on a background thread here.
if ([NSThread isMainThread]) {
[[self tableView] reloadData];
} else {
dispatch_sync(dispatch_get_main_queue(), ^{
[[self tableView] reloadData];
});
}
}
Run Code Online (Sandbox Code Playgroud)
并做了.您可以编写更多代码行,但它会替换使用ASI导入的13K +代码行,从而生成更小,更精简,更快的应用程序.更重要的是,它是一个应用程序,您了解每一行代码.
NWC*_*der 12
这就是问题
request setCompletionBlock:^{
self.listData = [[NSArray alloc] initWithObjects:@"Red", @"Green", @"Blue", @"Indigo", @"Violet", nil]; //this doesn't work...
[table performSelectorOnMainThread:@selector(reloadTable) withObject:nil waitUntilDone:NO];
}];
Run Code Online (Sandbox Code Playgroud)
重载表需要在主线程上完成.