Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-16-2011, 10:28 AM   #1 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 1,106
Meredi86 is on a distinguished road
Default Date in a TableView

Hey guys, I have a view (UIViewController <UITableViewDelegate, ...>) That has an individual cell on it. In this cell i want to have the current date displayed (upon the ViewLoading).

Currently i have this code in my cellForRowAtIndex:

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    date = [NSDate date];
    NSLog(@"DATE = %@", date);
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell ==nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellIdentifier] autorelease];
    }
    switch (indexPath.row) {
    case 0:
            cell.textLabel.text = @"Date";
            cell.detailTextLabel.text = [self.dateFormatter stringFromDate:date];
            break;
    }
    return cell;
}
But only the celltextlabel displays, the rest of the cell is empty. If i alter the detailtextlabel to be just a string(@"example") then that string will also display.

Is there something wrong with my above code, or does data not display if it is too long for the cell available?

Thanks in advance Joe
Meredi86 is offline   Reply With Quote
Old 02-16-2011, 10:57 AM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Show your date formatter code.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 02-17-2011, 03:12 AM   #3 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 1,106
Meredi86 is on a distinguished road
Default

Well currently dateFormatter is just a property. Declaserd in my .h file as:
Code:
@property (nonatomic, retain) NSDateFormatter *dateFormatter;
I assume then that i need to include more code then to format it in the cell correctly?
Meredi86 is offline   Reply With Quote
Old 02-17-2011, 03:15 AM   #4 (permalink)
- U haz disappoint -
 
Join Date: Jan 2010
Location: Belgium
Posts: 489
jNoxx is on a distinguished road
Send a message via MSN to jNoxx Send a message via Skype™ to jNoxx
Default

When I had this, i saw in my XIB that my tableview resized, and was too big, so it didn't display. So might check that x)
jNoxx is offline   Reply With Quote
Old 02-17-2011, 03:24 AM   #5 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 1,106
Meredi86 is on a distinguished road
Default

jnoxx - i had the same problem early on, trying to get a tableview to the right size in IB can be a bit of guess work!! However i had tested it with just simple strings as the tableview data. Thanks for the suggestion though

Brian - after i replied i went away and found what i needed. I had missed out "setDateFormat" then after including this it still didnt work, so i got rid of my properties for "date" and "dateformatter" and made them local variables. This solved my problem and now i can display the date as expected. Thanks for the nudge.

Edit

A bit of code as jNoxx requested

Code:
NSDate *date = [[NSDate alloc] init];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
This is what i needed to have set up to format my date. not too much to it, and of course you can alter the order of @"dd-MM-yyyy" till your hearts content (though being English dd-mm-yyyy is the correct way)

NSDateFormatter also handles the time with @"HH:mm:ss"

I found all my info over at SO, on this post: http://stackoverflow.com/questions/9...-in-iphone-sdk - credit where credit is due.

The code above in my original post for putting the date into a tablecell was correct, and i had to make no changes there - oh apart from not using self.dateFormatter - instead just drop the self.

Last edited by Meredi86; 02-17-2011 at 03:36 AM.
Meredi86 is offline   Reply With Quote
Old 02-17-2011, 03:27 AM   #6 (permalink)
- U haz disappoint -
 
Join Date: Jan 2010
Location: Belgium
Posts: 489
jNoxx is on a distinguished road
Send a message via MSN to jNoxx Send a message via Skype™ to jNoxx
Default

Lol ok, well, great
nice one ^_-
Btw, post some code, maybe people need it too
jNoxx is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 366
8 members and 358 guests
cpsclicker, Error404, jeroenkeij, Kirkout, PavelMik, teebee74, whitey99
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:06 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0