UITableView Cells with Formatted Text Parsed from an XML

I am Parsing an XML file from a URL using an NSXMLParser, the items that I parse are added to an array that a UITableView loads its data from. Among other things I am parsing titles for events and dates that those events take place on. These items are parsed and added to an array called stories. My goal is to have the title of an event be the main text for each cell in my TableView and the date be a subtitle. The title of each event does not need to be formatted (i.e. if the XML file reads: "Event One" then that is what should be displayed) I am successful with this using this method:

(title being the title of an item in the XML, stories being the array that the TableView loads its data from.)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]

    // Set up the cell
    NSUInteger storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
    cell.textLabel.text = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];

    return cell;

However I do need to format all of the dates I parse, I do so with an NSDateFormatter, I am satisfied with the format of the dates after the NSDateFormatter has done its job. If i did not need to format the dates I could use:

cell.detailTextLabel.text = [[stories objectAtIndex: storyIndex] objectForKey: @"isodate"];

to achieve my goal. Once the NSDateFormatter has done its job I have a variable (a NSMutableString) called currentDate with the string value for my formatted dates, using the console I can see that this yields the desired result for every item in the XML. When I use:

cell.textLabel.text = currentDate;

I end up with every cell in my table having the date of the final item in the XML file (granted it looks nice is formatted correctly). I have tried moving my methods around to no avail. If you wouldn't mind pointing me in the right direction, I need to know how to include the value of my variable currentDate in the subtitle text of a UITableView in a way in which the title of an item and the date of the item in a cell correspond with one another.


A couple of thoughts:

  1. Regarding your date problem, it sounds like you're storing the date in a single instance variable called currentDate. And given that you didn't share that code, I gather you're not doing that in cellForRowAtIndexPath, but rather, perhaps in your parser, or something like that. Unfortunately, that would give every cell in the table the date for the last row you parsed). You should either

    • call your date formatting right in cellForRowAtIndexPath (or, better, call a method that does your date formatting), specifically grab [[stories objectAtIndex: storyIndex] objectForKey: @"isodate"], format it, and set detailTextLabel.text accordingly; or

    • rather than storing the formatted date in a single currentDate variable, add an dictionary key for the formatted date and store it in the mutable dictionary with everything else you parsed from the XML (e.g. read in isodate, format a string and save it back to the same dictionary with a unique key, maybe formattedDate).

  2. NSUInteger storyIndex = indexPath.row; is a more common syntax if you're grabbing the news item associated with the given row of the tableview.

You can store your parse data into an NSDictionary with Key - value pair. Here you can use title as key and date as value and by this way both data will be corresponding to each one.

The problem is line

NSUInteger storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];

you should be using indexPath.row instead,

cell.textLabel.text = [[stories objectAtIndex: indexPath.row] objectForKey: @"title"];

Need Your Help

How can I tell the difference between a post from a browser, and someone trying to post programmatically

asp.net security browser viewstate

Is there a way to determine if the request coming to a handler (lets assume the handler responds to get and post) is being performed by a real browser versus a programmatic client?

java date to sql date type conversion

java sql casting

I am trying to convert a java date object to sql date object. I tried to implement something on my own and came up with the following: