Xcode iPhone Date Formatting issue
I can't for the life of me figure out why my code wont format the date when it has worked for me before so I figured I would post the code up and see if someone else could spot the issue..
- (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] autorelease];
}
// Set up the cell
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
cell.textLabel.text = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"];
NSDate *dateTmp;
dateTmp = [[stories objectAtIndex:storyIndex]objectForKey: @"date"];
cell.detailTextLabel.text = [dateFormat stringFromDate:dateTmp];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
cell.textLabel.font=[UIFont fontWithName:@"Verdana" size:12.0];
cell.detailTextLabel.textColor = Light_BACKGROUND;
[dateFormat release];
return cell;
}
I have used nslogs to see what information the code is actually making happen and it would seem that it has no problem getting the date from the mutable array but when I try to apply the stringFromDate for the detail text it just returns null so nothing is being placed leaving the detail text blank.
I attempted to set a string so I could check the log and this was the turn out.
[1984:207] all done!
[1984:207] stories array has 10 items
[1984:207] DateTmp = Wed, 16 Feb 2011 01:37:16 +0000
[1984:207] DateLine = (null)
[1984:207] DateTmp = Tue, 15 Feb 2011 23:05:31 +0000
[1984:207] DateLine = (null)
[1984:207] DateTmp = Tue, 15 Feb 2011 04:31:36 +0000
[1984:207] DateLine = (null)
[1984:207] DateTmp = Tue, 15 Feb 2011 02:16:0开发者_Python百科1 +0000
[1984:207] DateLine = (null)
[1984:207] DateTmp = Sun, 13 Feb 2011 04:07:42 +0000
[1984:207] DateLine = (null)
[1984:207] DateTmp = Sun, 13 Feb 2011 02:15:41 +0000
[1984:207] DateLine = (null)
[1984:207] DateTmp = Sun, 13 Feb 2011 00:21:42 +0000
[1984:207] DateLine = (null)
[1984:207] DateTmp = Sat, 12 Feb 2011 22:50:27 +0000
[1984:207] DateLine = (null)
[1984:207] DateTmp = Sat, 12 Feb 2011 21:07:27 +0000
[1984:207] DateLine = (null)
Thanks for the help.
Well it seems like you're creating the date right, but not retaining what you created. That is probably why you can NSLog the date value during creation. After the creation of the cell the string you made is out of scope, so you get (null). Try setting the cell text label like:
[[cell detailTextLabel] setText:[dateFormat stringFromDate:dateTmp]];
Your NSDateFormatter
's dateFormat
does not match the tmpDate you're showing in your log.
Try this:
[dateFormat setDateFormat:@"EEE, dd MMM yyyy hh:mm:ss ZZZ"]
Notice how your date format string is not taking the abbreviated day of the week into account and you have added dashes to the date where there are none.
Here's a page which shows all of the formatting characters used by NSDateFormatter
(OS X 10.5+ and iOS). Unicode standard tr35-6.
Found a work around that will be do, I wasn't able to find the reason I couldn't format the information my only guess left is that is has something to do with how the date is being stored when parsing the information from the twitter feed. Thanks to everyone who assisted me with the issue all of your solutions were good this just seemed to be a weird situation.
精彩评论