In my -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method I have this code to return youtube video date for cell label:
//Video upload date
NSString *formattedString = [[[self.videos objectAtIndex:indexPath.row] objectForKey:@"uploaded"] stringByReplacingOccurrencesOfString:@"T" withString:@""];
formattedString = [formattedString stringByReplacingCharactersInRange:NSMakeRange(18, 5) withString:@""];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-ddHH:mm:ss"];
NSDate *date = [df dateFromString:formattedString];
[df setDateFormat:@"dd/MM/yyyy"];
NSString *dateStr = [df stringFromDate:date];
cell.date.text = dateStr;
My method implementation looks bit messy and I am wondering what is correct approach. Should I keep this code where it is or I should make another private method for the date formatting?
Let say I want to make private method. Something like:
-(NSString *)formatDateWithString:(NSString *) mystring {
NSString *formattedString = [mystring stringByReplacingOccurrencesOfString:@"T" withString:@""];
formattedString = [formattedString stringByReplacingCharactersInRange:NSMakeRange(18, 5) withString:@""];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-ddHH:mm:ss"];
NSDate *date = [df dateFromString:formattedString];
[df setDateFormat:@"dd/MM/yyyy"];
NSString *dateStr = [df stringFromDate:date];
return dateStr;
}
What should I than write in -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method?
My argument for my private method will be [self.videos objectAtIndex:indexPath.row]
but I've lost how do I call this method and on which instance variable?
I need to create an NSString
instance variable in my view class or I can just initiate NSString instance in the -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method and execute my private method on it?