最初的目标是使用NSDateComponents计算两个NSDates之间的差异 . 此外,一旦获得日期,小时,分钟和秒,并验证为正确 . 显示UITableViewCell中差异(到最近的分钟,小时或天)的实时自动收报机(计数器) . 目前的进展如下 . 任何建议或实现这一目标的方向将不胜感激!
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =@"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [(AGSGraphic *)[_radios objectAtIndex:indexPath.row] attributeForKey:@"parent_id"];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
NSNumber *timestamp = [[_radios objectAtIndex:indexPath.row] valueForKey:@"date_time_edt"];
NSDate *today = [NSDate date];
NSDate *crewDate = [NSDate dateWithTimeIntervalSince1970:[timestamp doubleValue]/1000];
NSString *temp = [df stringFromDate:today];
NSString *crewtemp = [df stringFromDate:crewDate];
crewDate = [df dateFromString:crewtemp];
today = [df dateFromString:temp];
NSLog(@"%@", crewDate);
NSLog(@"%@", today);
NSTimeInterval interval = [crewDate timeIntervalSinceDate:today];
interval = -interval;
NSInteger minutes = interval/60;
NSInteger hours = minutes/60;
NSInteger days = hours/24;
// NSInteger weeks = days/7;
// NSInteger months = weeks/4;
// NSInteger years = months/12;
// NSTimeInterval years = interval/31536000;
// NSTimeInterval months = years/12;
// NSTimeInterval weeks = years/52;
// NSTimeInterval days = years/365;
// NSTimeInterval hours = days/24;
// NSTimeInterval minutes = hours/60;
// NSLog(@"%f", interval);
if(minutes < 60)
{
cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld minuntes ago", (long)minutes];
}
else if(hours < 24)
{
cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld hours ago", (long)hours];
}
else
{
cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld days ago", (long)days];
}
return cell;
}
1 回答
这是你如何使用分钟来做到这一点:
此外,我只假设使用我使用的分钟方法,您还可以使用其他方法通过使用NSCalendar.h中提供的选项交换NSCalendarUnitMinute来计算不同日期测量单位的差异,请参阅下文: