如何在特定时间显示图像?

时间:2016-05-10 06:44:22

标签: ios objective-c

我有TableViewController和ViewController。在ViewController中,我节省了时间,在TableViewController中,如果时间超过1:00,我想在表格单元格中显示图像。但我的代码不起作用...请帮帮我。

TableViewController中的代码

_timeElapsed.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"time0"];

if (self.timeElapsed.text  >= @"1:00") {
    UIImageView *imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(28.5, 23, 25, 25)];
    imageView2.backgroundColor = [UIColor clearColor];
    [imageView2.layer setMasksToBounds:YES];
    imageView2.tag = 3;
    [cell.contentView addSubview:imageView2];
    UIImageView *imgView2 = (UIImageView *)[cell.contentView viewWithTag:3];
    imgView2.image = [UIImage imageNamed:@"accessory.png"];
}

@seto nugroho

现在我的_timeElapsed.text = 2:57但是图片没有显示

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: [NSString stringWithFormat:@"Cell%d", indexPath.row] forIndexPath:indexPath];



_timeElapsed.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"time0"];

NSString *stringFromInput = self.timeElapsed.text;
NSString *stringToCompare = @"1:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"m:ss";

NSDate *timeFromInput = [dateFormatter dateFromString:stringFromInput];
NSDate *timeToCompare = [dateFormatter dateFromString:stringToCompare];

if ([timeFromInput compare:timeToCompare] == NSOrderedDescending) {
    //actual time is greater
    UIImageView *imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(28.5, 23, 25, 25)];
    imageView2.backgroundColor = [UIColor clearColor];
    [imageView2.layer setMasksToBounds:YES];
    imageView2.tag = 3;
    [cell.contentView addSubview:imageView2];
    UIImageView *imgView2 = (UIImageView *)[cell.contentView viewWithTag:3];
    imgView2.image = [UIImage imageNamed:@"accessory.png"];
} else {
    //not greater than 1 minutes

}

[tableView setSeparatorInset:UIEdgeInsetsMake(0, 70, 0, 1)];
return cell;
}

@iOS Geek

如果timeString = @"0:05"if (timeInSeconds > 1)图片没有显示,但在这种情况下,我会看到图片。

NSString *timeString = @"0:05";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"mm:ss";
NSDate *timeDate = [formatter dateFromString:timeString];

formatter.dateFormat = @"mm";
int minutes = [[formatter stringFromDate:timeDate] intValue];
formatter.dateFormat = @"ss";
int seconds = [[formatter stringFromDate:timeDate] intValue];

int timeInSeconds = seconds + minutes * 60;

if (timeInSeconds > 1) {
    UIImageView *imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(28.5, 23, 25, 25)];
    imageView2.backgroundColor = [UIColor clearColor];
    [imageView2.layer setMasksToBounds:YES];
    imageView2.tag = 3;
    [cell.contentView addSubview:imageView2];
    UIImageView *imgView2 = (UIImageView *)[cell.contentView viewWithTag:3];
    imgView2.image = [UIImage imageNamed:@"accessory.png"];
}

4 个答案:

答案 0 :(得分:0)

您无法比较字符串值,除非您想要查看它们是否逐字符(在这种情况下,您使用“isEqualToString:”)。 将时间转换为整数值,然后进行比较。

<强>更新

您可以使用NSDateFormatter从时间字符串中获取秒和分钟:

NSString *timeString = @"3:31";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"mm:ss";
NSDate *timeDate = [formatter dateFromString:timeString];

formatter.dateFormat = @"mm";
int minutes = [[formatter stringFromDate:timeDate] intValue];
formatter.dateFormat = @"ss";
int seconds = [[formatter stringFromDate:timeDate] intValue];

int timeInSeconds = seconds + minutes * 60;

答案 1 :(得分:0)

比较这样的时间是不好的:

self.timeElapsed.text  >= @"1:00"

请将其转换为NSDateInt(不太好但很简单)。像这样:

[[self.timeElapsed.text componentsSeparatedByString:@":"] firstObject].intValue > 1

答案 2 :(得分:0)

您的代码无法正常工作,因为您正在比较String,这显然不是您想要的 您应该将您的字符串转换为日期,然后进行比较。

NSString *stringFromInput = self.timeElapsed.text;
NSString *stringToCompare = @"1:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"m:ss";

NSDate *timeFromInput = [dateFormatter dateFromString:stringFromInput];
NSDate *timeToCompare = [dateFormatter dateFromString:stringToCompare];

if ([timeFromInput compare:timeToCompare] == NSOrderedDescending) {
    //actual time is greater
} else {
    //not greater than 1 minutes
}

答案 3 :(得分:0)

打开警告。编译器会警告您将指针与&gt; =运算符进行比较,这通常是未定义的行为,并且通常是无意义的,如您的情况。

self.timeElapsed.text  >= @"1:00"

比较两个指针。它比较指向存储对象self.timeElapsed.text的位置的指针,以及指向对象@&#34; 1:00&#34;的位置的指针。被储存了。这显然是无意义的,与对象的实际值完全无关。