0

我正在尝试使用情节提要和 JSON 创建一个松散版本的 LazyTabelImages。在我的主 TableViewController 上的 ViewDidLoad 中,我启动了一个 NSURLConnection 来获取 JSON 数据,但我的单元格直到连接完成后才会加载。我想要与 LazyTableImages 相同的行为,其中单元格加载为空白,然后填充信息(重新加载表数据)。如果我不使用情节提要,我可以复制它,因为 LazyTables 不使用情节提要,但这不是一个选项。

我查看了 LazyTableImages 试图找到解决方案,但故事板有很大的不同(无论如何对我来说)。

有没有一种简单的方法可以让单元格加载为空白?例如,如果设备没有互联网,我仍然希望我的 TableView 显示,我会在单元格中放置一条自定义消息。

代码:

我在 viewDidLoad 中初始化连接的部分......

NSURLRequest *urlrequest = [NSURLRequest requestWithURL:[NSURL URLWithString:serverURL]];
    self.dataConnection = [[NSURLConnection alloc] initWithRequest:urlrequest delegate:self];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

connectionDidFinnishLoading...

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //ListData below is an array that my data received (JSON) is loaded into. It is then passed to getTableData.
    self.dataConnection = nil;
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self performSelectorOnMainThread:@selector(getTableData:) withObject:ListData waitUntilDone:YES];
    });
}

获取表数据...

-(void)getTableData:(NSData *)jsonData
{
    NSError *error = nil;
    arrayEntries = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:&error];
    for (int x = 0; x < arrayEntries.count; x++)
    {
        NSMutableDictionary *dic = [arrayEntries objectAtIndex:x];

        //ARecord is a class just like in LazyTableImages that creates objects to keep the icons/data together. The ARecords are loaded into the TableView
        ARecord *arecord = [[ARecord alloc] init];

        NSString *title = [dic objectForKey:@"title"];
        NSString *subt = [dic objectForKey:@"subtitle"];
        NSString *url = [dic objectForKey:@"image_URL"];
        arecord.Icon = nil;
        arecord.URL = url;
        arecord.Name = title;
        arecord.title = subt;

        //this is where I load an array full of the arecord objects.
        [array addObject:arecord];
    }
    [self.tableView reloadData];
}
4

3 回答 3

1

我用两个对象做这个。首先,我有一个图像获取器类,它异步下载数据并在完成时通知委托。然后我有一个实现 fetcher 的委托方法的图像视图类。所以像:

@implementation AsyncImageFetcher
-(id)initWithURL:(NSURL *)aURL andDelegate:(id<SomeProtocol>)aDelegate{

  //...

  NSURLRequest *req = [NSURLRequest requestWithURL:aURL];
  //Note that NSURLConnection retains its delegate until the connection
  //terminates! See comments in MyImageView below.
  [NSURLConnection connectionWithRequest:req delegate:self];

  //...
}

//Implement standard connection delegates here. The important part is:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{

  // ...

  UIImage *anImage = [self decodeDownloadedDataIntoAnImage];
  if([[self delegate] respondsToSelector:@selector(imageFetcher:didFetchImage:)]){
    [[self delegate] imageFetcher:self didFetchImage:anImage];
  }

  //...
}

@end

然后我继承UIImageVieworUIView或其他东西(取决于你需要多么灵活)来实现委托协议并触发 fetcher:

@implementation MyImageView

-(id)initWithURL:(NSURL *)aURL andPlaceHolderImage:(UIImage *)aPlaceHolder{

  //...

  [self setImage:aPlaceHolder];

  //Note we don't assign this to an ivar or retain it or anything. 
  //That's ok because the NSURLConnection inside the fetcher actually 
  //retains the fetcher itself. So it will live until the connection 
  //terminates -- which is exactly what we want. Thus we disable 
  //CLANG's noisy warnings.
  #pragma clang diagnostic push
  #pragma clang diagnostic ignored "-Wunused-value"
  [[AsyncImageFetcher alloc] initWithURL:aURL andDelegate:self];
  #pragma clang diagnostic pop

  return self;
}


-(void)imageFetcher:(MCMAsyncImageFetcher *)anImageFetcher didFetchImage:(UIImage *)anImage{
  [self setImage:anImage];
}

@end

在您的特定情况下,您只需将 a 设置MyImageView为您的单元格imageViewin -tableView:cellForRowAtIndexPath:,当然,为其占位符和 URL 传递合理的值。

于 2013-01-17T03:51:13.100 回答
1

我做过类似的事情。在 viewDidLoad 中:我将表数据的数组设置为 [NSNull null] 的几个对象,但我想在数据下载时显示许多空白行。在 cellForRowAtIndexPath: 我检查 [self.arrayOfTableData objectAtIndex:indexPath.row] = [NSNull null]。如果是,则返回一个“空白”单元格,否则使用 ARRecrod 数据加载该单元格。然后,当 URL 完成时,将 NSNulls 数组替换为您的 ARRecords 数组。

于 2013-01-18T18:23:11.007 回答
0

由于我没有看到你的代码,我只是在这里给出我的建议:

- (void)viewDidLoad
{
    [super viewDidLoad];

    dispatch_queue_t queue = dispatch_queue_create(NULL, NULL);
    dispatch_async(queue, ^{

        //add your connection code here
        //parse the json and store the data
        //

        dispatch_async(dispatch_get_main_queue(), ^{

            //here to reload your table view again, 
            //since UI related method should run on main thread.

            [YOUR_TABLEVIEW reloadData];

        });

    });

    [YOUR_TABLEVIEW reloadData];
}

注意:确保故事板中的表格视图已连接到代码中的表格视图!希望能帮助到你!

于 2013-01-17T03:14:08.850 回答