iOS使用RAC在UITableViewCell中加载图像
我刚开始使用RAC,我想知道如何在TableView中的单元格中异步加载图像。 我试着在博士的例子,但老实说,我不太明白。。。 问题是这个项目是用RAC编写的,所以我想做正确的事情 我试过什么了iOS使用RAC在UITableViewCell中加载图像,ios,asynchronous,uitableview,uiimage,reactive-cocoa,Ios,Asynchronous,Uitableview,Uiimage,Reactive Cocoa,我刚开始使用RAC,我想知道如何在TableView中的单元格中异步加载图像。 我试着在博士的例子,但老实说,我不太明白。。。 问题是这个项目是用RAC编写的,所以我想做正确的事情 我试过什么了 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"tableCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"tableCell";
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.elementName.text = self.myListOfElements[indexPath.row].elementName;
RAC(cell.imageView, image) = [[finalImage map:^(NSURL *url) {
return [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.myListOfElements[indexPath.row].url]]];
}] deliverOn:RACScheduler.mainThreadScheduler];
}
但这不起作用。。。
有人知道使用RAC的正确方法吗?我从未使用过RAC,但根据示例,您似乎正在引用一个您似乎没有的URL
RAC(self.imageView, image) =
[
[
[
[
client fetchUserWithUsername:@"joshaber"
]
deliverOn:[RACScheduler scheduler]
]
map:^(User *user) {
// Download the avatar (this is done on a background queue).
return [[NSImage alloc] initWithContentsOfURL:user.avatarURL];
}
]
// Now the assignment will be done on the main thread.
deliverOn:RACScheduler.mainThreadScheduler
];
在本节的示例中:
[
[
client fetchUserWithUsername:@"joshaber"
]
deliverOn:[RACScheduler scheduler]
]
-fetchUserWithUsername是返回包含用于下载图像的URL的用户对象的函数。此行假定已经在CustomTableViewCell的
imageView属性上创建并设置了UIImageView对象:
RAC(cell.imageView, image) = [[finalImage map:^(NSURL *url) {
假定此UIImageView对象是在CustomTableViewCell类的初始值设定项中或在-prepareForReuse
方法中创建的。如果不是,那可能是你问题的一部分
但是,请注意,此代码看起来并不那么安全。如果重用CustomTableViewCell实例,那么您可能会再次调用对象上的RAC(cell.imageView,image)
,这将是一个问题。(另一方面,如果-prepareforeuse
每次都在创建一个新的UIImageView,那么这不应该是一个问题。)答案不完全正确,但感谢您的“给我指示灯”:)在您编写的代码中,客户端应该是一个信号,返回一个包含数据的对象。