0

Am just starting with RAC and am wondering how to load images asynchronously in a cell within a TableView. I was trying with the example in the doc, but to be honest, I didn't understand so well... The thing is that the project is written with RAC, so I want to do the right things.

What have I tried?:

- (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];
}

But this is not working... Does anybody know the proper way to do this with RAC?

FelipeDev.-
  • 3,113
  • 3
  • 22
  • 32

2 Answers2

0

I have never used RAC, but based on the examples it looks you are referencing a URL that you do not seem have.

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
];

In the example this section:

[
    [
        client fetchUserWithUsername:@"joshaber"
    ]
    deliverOn:[RACScheduler scheduler]
]

-fetchUserWithUsername is the function that returns the User object that contains the URL used to download the image.

blueice
  • 136
  • 6
  • Not exactly the answer, but thanks for "show me the light" :) In the code you wrote, client should be a signal that returns an Object with your data. – FelipeDev.- Mar 31 '14 at 15:39
0

This line assumes that there is already a UIImageView object created and set on the CustomTableViewCell's imageView property:

  RAC(cell.imageView, image) = [[finalImage map:^(NSURL *url) {

Presumably this UIImageView object is being created in the CustomTableViewCell class's initializer or in -prepareForReuse method. If it's not, that could be part of your problem.

Be aware, though, that this code doesn't look that safe. If a CustomTableViewCell instance is reused, then you could end up calling RAC(cell.imageView, image) on the object a second time, which would be a problem. (On the other hand, if -prepareForReuse is creating a new UIImageView each time, then this shouldn't be a problem.)

erikprice
  • 6,240
  • 3
  • 30
  • 40