2016-06-16 43 views
1

我想将我的NSDate放入单元格内的UILabel。现在我有这个问题将NSDate放入UILabel问题

***终止应用程序由于未捕获的异常 'NSInvalidArgumentException',原因是: ' - [__ NSTaggedDate rangeOfCharacterFromSet:]:无法识别的选择发送到实例0xe41bd157d5000000'

这里是我已经做了迄今为止

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 50, 130)]; 
NSDate *date = [[myArray objectAtIndex:indexPath.row] objectForKey:@"Date"]; 
typeLabel.tag =2; 
typeLabel.text = date; 

[[cell.contentView viewWithTag:2]removeFromSuperview]; 
[cell.contentView addSubview:dateLabel]; 

return cell; 
} 

{ 
    Start = [[[[array objectAtIndex:0] valueForKey:@"StartTime"]objectAtIndex:index]valueForKey:@"text"]; 
    Start = [Start stringByReplacingOccurrencesOfString:@"\n" withString:@""]; 
    Start = [Start stringByReplacingOccurrencesOfString:@"\t" withString:@""]; 
    Stream = [Stream stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
    NSString* format = @"yyyy-MM-dd HH:mm:ss"; 

    NSDateFormatter* formatterUtc = [[NSDateFormatter alloc] init]; 
    [formatterUtc setDateFormat:format]; 
    [formatterUtc setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]]; 

    // Cast the input string to NSDate 
    NSDate* utcDate = [formatterUtc dateFromString:Start]; 

    // Set up an NSDateFormatter for the device's local time zone 
    NSDateFormatter* formatterLocal = [[NSDateFormatter alloc] init]; 
    [formatterLocal setDateFormat:format]; 
    [formatterLocal setTimeZone:[NSTimeZone localTimeZone]]; 

    // Create local NSDate with time zone difference 
    NSDate* localDate = [formatterUtc dateFromString:[formatterLocal stringFromDate:utcDate]]; 

    NSLog(@"utc: %@", utcDate); 
    NSLog(@"local: %@", localDate); 

    [dict setObject:localDate forKey:@"Date"]; 
    [myArray addObject:dict]; 
} 

回答

1

您不能分配NSDate直接的文字,使用NSDateFormatter其转换为NSString,然后分配给它。 UILabel的text属性是NSString类型的。你正在分配NSDate。

0

您正在为NSString对象分配NSDate对象。首先创建一个NSDateFormatter对象,并将日期分配给像这样的字符串。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"dd-MM-yyyy''HH:mm"]; 
NSString *formattedDateString = [dateFormatter stringFromDate:date]; 
0

将您NSDateNSString,比它存储到字典如下:

NSDate* localDate = [formatterUtc dateFromString:[formatterLocal stringFromDate:utcDate]]; 

    NSString *strLocalDate=[formatterUtc stringFromDate:localDate]; 

    [dict setObject:strLocalDate forKey:@"Date"]; 
    [myArray addObject:dict];