0


I am trying to create a basic app that displays event information from a server.
So far I download all the information in an xml file and parse it using NSXMLParser.
I have an event object that I have created that stores all of the information for each event, I then populate a NSMutableArray with these events.
I would like to then asynchronously load the images relating to each event as each UITableView Cell displays, and store them in a NSData in the event objects located within the NSMutableArray. This way I would only load images that I need.
The layout of my NSMutableArray looks like this(simplified):

events
      -event1
            -(NSString *)title
            -(NSURL *)imageURL
            -(NSData *)eventData
      -event2
            -(NSString *)title
            -(NSURL *)imageURL
            -(NSData *)eventData
etc..

I have no idea what the best practice here is and am struggling to make it work.

  • Option 1
    I can try to set it straight from the MasterViewController, this doesn't seem right to me, and I get an error. If I use the code

    [[events objectAtIndex:cellIndexPath.row] eventImageData] = [[NSData alloc] initWithContentsOfURL:[[events objectAtIndex:cellIndexPath.row] eventImgURL]];
    

I get the error: expression not assignable

  • Option 2
    I can write a method within the event object itself to download the image. I did this, and for the life of me couldn't get the event object to ask the MasterViewController to refresh the appropriate NSTableViewCell once it had finished getting the image.

My question is, am I on the right track with either of these trains of thought? Can I allocate a new NSData inside of an object within a NSMutableArray? or was I on the right track asking the event object to download it's own image?
Thanks for taking the time to read this overly-long-winded question.

2 Answers 2

3

You can't use a getter method to set a property, you have to use a setter method:

[[events objectAtIndex:cellIndexPath.row] setEventImageData:[[NSData alloc] initWithContentsOfURL:[[events objectAtIndex:cellIndexPath.row] eventImgURL]]];
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! I'm kicking myself now, it's always something simple isn't it! Cheers for taking the time to help a newbie out.
@Hockey yes it's pretty simple :)
1

Another alternative to modify an object within a NSMutableArray is:

if([[events objectAtIndex:cellIndexPath.row] count] >=2)
{
  [[events objectAtIndex:cellIndexPath.row] replaceObjectAtIndex:2  withObject:[[NSData alloc] initWithContentsOfURL:[[events objectAtIndex:cellIndexPath.row] eventImgURL]];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.