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]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:MyIdentifier];
}
// 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:
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 incellForRowAtIndexPath, 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 eithercall 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 setdetailTextLabel.textaccordingly; orrather than storing the formatted date in a single
currentDatevariable, 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 inisodate, format a string and save it back to the same dictionary with a unique key, maybeformattedDate).NSUInteger storyIndex = indexPath.row;is a more common syntax if you’re grabbing the news item associated with the given row of the tableview.