Question

I am polling an HTTP API - it returns one item at a time, in real-time (about every 4 seconds). As each item is received, I would like a new UITableView cell to be populated. The full list of received items must remain in a class property, I'm guessing an NSMutableArray. What is the best way to initialize an NSMutableArray as a class property, update it as new information comes in, and then use the count to update a new UITableViewCell? Here's how I'm adding content to an NSMutableDictionary:

NSMutableDictionary *messageContents = [[NSMutableDictionary alloc] init];
[messageContents retain];
[messageContents setValue:messageText forKey:@"text"];
[messageContents setValue:image forKey:@"image"];
[self addMessageToDataArray:messageContents];

Here's the method stuffing objects into the array:

- (void)addMessageToDataArray:(NSArray *)messageDictionary {
    [self.messageDataArray addObject:messageDictionary];
    NSLog(@"count = %@", [self.messageDataArray count]);
    [self reloadTableData];
}

At this point, calling count on the messageDataArray class property crashes the application. I'm very used to working with arrays in Actionscript, Obj-C is obviously totally different. Please explain the method for instantiating an NSMutableArray as a class property, filling it with NSMutableDictionary's and then finding the NSMutableArray count (which will be dynamically updating in real-time) so I can use that info to update a UITableView (on the fly).

Or... tell me I'm being silly and suggest a much easier solution.

Was it helpful?

Solution

From your description I would guess you're not allocating the messageDataArray before using it.

The init function for your table view (controller?) class should have a line like this

messageDataArray = [[NSMutableArray alloc] initWithCapacity:20];

It's also worth checking that you have [messageDataArray release]; in your dealloc method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top