Reputation: 331
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
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.
Upvotes: 0
Views: 158
Reputation: 38239
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]];
}
Upvotes: 1
Reputation:
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]]];
Upvotes: 3