I have a app which has a download manager built into it. When a file is downloaded, it gets saved to a folder inside of the documents folder. Now I have implemented a time and date stamp in my table view cell. However, When switching from one view controller back to my table view, the time and date updates to the current time.
Below is my code I'm working with, and as always any help would be appreciated.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } //did the subtitle style
NSUInteger row = [indexPath row];
cell.textLabel.text = [directoryContents objectAtIndex:row];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, MMM/d/yyyy, hh:mm aaa"];
NSDate *date = nil;
if (indexPath.row == 0)
{
date = [NSDate date];
NSString *dateString = [dateFormatter stringFromDate:date];
cell.detailTextLabel.text = dateString;
}
return cell;
}
tableView:cellForRowAtIndexPath: will be called every time the table is reloaded, and hence the time will be updated to the current time because [NSDate date] will be called again.
Instead of calculating the date in tableView:cellForRowAtIndexPath: you should store it elsewhere (such as in an class level NSMutableArray) and set it when the file finishes downloading. This way it won't be reset each time the table loads.
Decided to circle back to this. I ended up getting it figured out (not that I was working on it this whole time lol).
Anyways heres what I did:
//Setting the date
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"my folder"];
filePath = [filePath stringByAppendingPathComponent:fileName];
NSDate *creationDate = nil;
NSDictionary *attributes = [fileManager attributesOfItemAtPath:filePath error:nil];
creationDate = attributes[NSFileCreationDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy"];
NSString *dateString = [dateFormatter stringFromDate:creationDate];
cell.detailTextLabel.text = dateString;
Hope it helps somebody.
Related
I'm developing an iOS app using objective-c. I'm having custom cell in UITableview. the section and rows in section count is unknown every time ,
the problem I'm facing is each row in one section repeats 13 times then same with next section my code for tableview is
static NSString *cellIdentifier = #"Cell";
ProductCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[ProductCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSDate *dateRepresentingThisDay = [self.sorteddays objectAtIndex:indexPath.section];
eventsOnThisDay = [self.sections objectForKey:dateRepresentingThisDay];
double interval =[[eventsOnThisDay objectAtIndex:indexOfDateWithTime] doubleValue];
NSDate *celldate=[[NSDate alloc]initWithTimeIntervalSince1970:interval];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"dd-MMM-yyyy h:mm a"];
cell.TitleLabel.text=[NSString stringWithFormat:#"%#", [eventsOnThisDay objectAtIndex:indexOfTitle]];
cell.DateTimeLabel.text=[NSString stringWithFormat:#"%#", [dateFormatter stringFromDate:celldate]];
cell.NameLabel.text=[NSString stringWithFormat:#"%#", [eventsOnThisDay objectAtIndex:indexOfName]];
cell.backgroundColor=[UIColor clearColor];
return cell;
self.section is NSDictionary with date key for particular product array/arrays on that day and i am getting this kind of result
I notice that the data for display for every row is based on dateRepresentingThisDay,but the value of it, is based on section. Check your code:
NSDate *dateRepresentingThisDay = [self.sorteddays objectAtIndex:indexPath.section];
So, in the same section, for all cell's the data is the same!
So I have a download manager in my app. I have taken it upon myself to liven it up today.
I have implemented UITableViewCellStyleSubtitle, and is displaying properly.
I want to add more than 1 line to it. Right now I'm stuck in choosing either the file size or the formatted date.
How would I do both? i.e.
Cell Title
Date: (followed by file size) or
File Size:
Below is the relevant code I'm working with.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [(UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier] autorelease];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
}
// Configure the cell.
cell.textLabel.text = [directoryContents objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryNone;
[cell.layer setBorderColor:[UIColor colorWithRed:30/255.0 green:30/255.0 blue:30/255.0 alpha:1.0].CGColor];
[cell.layer setBorderWidth:1.5f];
cell.textLabel.numberOfLines = 0;
//Get file size
NSError *error;
NSString *fileName = [directoryContents objectAtIndex:indexPath.row];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"my folder"];
path = [path stringByAppendingPathComponent:fileName];
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error];
NSInteger fileSize = [[fileAttributes objectForKey:NSFileSize] intValue];
//Setting the date
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"my folder"];
filePath = [filePath stringByAppendingPathComponent:fileName];
NSDate *creationDate = nil;
NSDictionary *attributes = [fileManager attributesOfItemAtPath:filePath error:nil];
creationDate = attributes[NSFileCreationDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy"];
NSString *dateString = [dateFormatter stringFromDate:creationDate];
/////This is where I need to blend the dateString with the file size//////
cell.detailTextLabel.text = [NSString stringWithFormat:dateString, #"%#", [self formattedFileSize:fileSize]];
cell.detailTextLabel.numberOfLines = 2;
return cell;
}
Thank you in advanced.
I tried this out, and for some reason, setting numberOfLines to 2 didn't work for me either, but setting it to anything greater then 2, or setting it to 0 worked.
You need to format your two strings properly. This is not correct syntax,
[NSString stringWithFormat:dateString, #"%#", [self formattedFileSize:fileSize]]
It should be like this,
[NSString stringWithFormat:#"%#\n%#", dateString, [self formattedFileSize:fileSize]];
Figured this out. Just had to tweak around with the format of the string.
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#\n%#", dateString, [self formattedFileSize:fileSize]];
As posting this answer the page refreshed and seen that rdelmar is also correct in his updated post showing the proper syntax.
Credit and thanks to him for the help in helping me think this out.
My problem is I have a Custom Cell which contains a label.
The value of label would be fetched via webServices which would be in TIME .Initially if we go by static data i.e. for e.g.: 10:54 PM, then on click of that cell it should set me a reminder of that value set on label.And off course should notify me on the said time.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
CC *cell=(CC*)[tableView cellForRowAtIndexPath:indexPath];
NSDate *currentDate=[NSDate date];
NSString *dateStr= cell.timeLabel.text;
UILocalNotification *localNotification =[[UILocalNotification alloc]init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:#"hh:mm a"];
[dateFormatter setTimeZone:gmt];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString *preFix=[dateFormatter stringFromDate:currentDate];
NSString *datetoFire=[NSString stringWithFormat:#"%# %#",preFix,dateStr];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm a"];
NSLog(#"date is %#",[dateFormatter dateFromString:datetoFire]);
if (localNotification==nil) {
return;
}
localNotification.fireDate=[dateFormatter dateFromString:datetoFire];
localNotification.timeZone=[NSTimeZone defaultTimeZone];
localNotification.alertBody=#"Good Morning dude..!";
localNotification.repeatInterval=0; //The default value is 0, which means don't repeat.
localNotification.soundName=UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CC *cell = (CC *) [tableView dequeueReusableCellWithIdentifier:#"CC"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CC" owner:self options:nil];
cell = (CC *) [nib objectAtIndex:0];
}
return cell;
}
I tried doing this, but this isn't working.
Thank You.
Take a dynamic array that will contain values populated from webservice and put populated time to cell dynamically in your cell.In cellForRow method.
I have taken static array as given below
time = [[NSMutableArray alloc] initWithObjects:#"5:35 PM",#"4:56 AM",#"6:00 PM",#"7:32 AM",nil];
you have to put values in this array dynamically as u will get populated data from webservice.
and reload table.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CC *cell = (CC *) [tableView dequeueReusableCellWithIdentifier:#"CC"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CC" owner:self options:nil];
cell = (CC *) [nib objectAtIndex:0];
}
cell.textLabel.text=[time objectAtIndex:indexPath.row];
return cell;
}
And in did select method assign fire date from array not from cell text label as
NSString *dateStr=[time objectAtIndex:indexPath.row];
i am new in iphone devlpmnt.i just want to add current date in the cell. i need "notiftext" appear left of the cell and current date on the right.thanks in advance
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AppDelegate *AppD = (AppDelegate *)[[UIApplication sharedApplication]delegate];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]autorelease];
}
MLNotifMessage *list = [AppD.storage objectAtIndex:indexPath.row];
cell.textLabel.text = list.notifText;
NSString *dteStr = [[NSString alloc]init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/London"]];
[dateFormat setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
dteStr = [dateFormat stringFromDate:list.date];
[dateFormat release];
cell.detailTextLabel.text = dteStr;
return cell;
}
Use any other style of the cell or use custom cell to fully customize the cell. I think for your question tabel view stye UITableViewCellStyleValue1 is enough.
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]autorelease];
cell.textLabel.text = list.notifText;
cell.detailTextLabel.text = [NSDate date];
use Custom TableViewCell. You can make custom tableViewCell by subclassing it
I have a problem with using an NSMutableArray and NSMutableDictionary.
I've created a quiz that when it stops, it should display the current score and date in a tableview. It should then display those values in a tableview. But nothing is displayed in the tableview when I load its viewController. The mutableArray is created in the viewDidLoad: method. Is my code correct, or am I missing something here?
- (IBAction)saveResult:(id)sender {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:NSLocalizedString(#"DATE", nil)];// #"HH:mm zz"];
dateNow = [NSDate date];
NSString *formattedDateString = [dateFormatter stringFromDate:dateNow];
// Add the quiz result to the array, reload the data in the tableview
highscoreAndDate = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:#"%i points", score], #"kResult",
formattedDateString, #"kDate", nil];
[highscore addObject:highscoreAndDate];
[tableView reloadData];
}
My tableView methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [highscore count];
}
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat: #"HH:mm zz"];
cell.detailTextLabel.text = [[highscore objectAtIndex:indexPath.row] objectForKey:#"kResult"];
cell.detailTextLabel.text = [[highscore objectAtIndex:indexPath.row] objectForKey:#"kDate"];
return cell;
}
#Anoop Vaidya.
You got me going on the right track. I put the NSLog statement when the objects are added and it shows that the row count of my array is going up. So my code is correct.
I figured out that it was due to my connections in my XIB file (very embarrassing!). Should have spotted that my self. I had hooked up my tableview outlets to the wrong viewController and so the tableview was never populated.
Very sorry for have taken up your time and thanks to everybody who responded so quickly. Glad I got it fixed. :)