Reputation: 1479
I've got a UITableView
that displays dynamic information based on a number of factors inside labels. The labels in turn are properties of the custom UITableViewCell
used as a reusable cell. Everything seems to work fine except for one pesky label, self.thisCustomCell.fromDateLabel
.
This label should reflect whether the timed interval of a particular activity overlaps or does not overlap the starting time of a selected search timeframe. If it does, the label should read "STARTED EARLIER". If it does not, the label should read the actual startTime
of the activity in question. In a practical sense, under ordinary circumstances, the chronologically oldest (bottom-most) label should always say "STARTED EARLIER".
However, this label's behavior is erratic, whether it is in the oldest cell or another cell. Meaning that the oldest cell (whose duration should always overlap the start tie of the timeframe) usually gives the startTime
instead, while other cells, which should never display "STARTED EARLIER", sometimes do.
Here are a couple of screenshots to illustrate the problem:
The order is descending, chronologically, and the bottom-most cell in the first shot straddles the timeframe start time, therefore the lower righthand label should read "STARTED EARLIER".
In this shot, you can see that the label in other cells DOES read "STARTED EARLIER", even though it should not.
Here's what I believe to be the relevant code:
-(void) updateOtherLabels
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat: @"MM/dd/yy hh:mm a"];
if ((thisActivity.startTime < self.detailStartDate) && (thisActivity.stopTime > self.detailStartDate))
{
self.thisCustomCell.fromDateLabel.text = @"STARTED EARLIER";
NSLog(@"FromDateLabel text is %@",self.thisCustomCell.fromDateLabel.text);
NSLog(@"self.detailStartDate is %@",[dateFormat stringFromDate:thisActivity.startTime]);
}
else
{
self.thisCustomCell.fromDateLabel.text = [dateFormat stringFromDate:thisActivity.startTime];
}
if (thisActivity.stopTime == NULL)
{
self.thisCustomCell.toDateLabel.text = @"RUNNING";
}
else
{
self.thisCustomCell.toDateLabel.text = [dateFormat stringFromDate:thisActivity.stopTime];
}
}
Can someone please point out what I've done wrong? An extensive search on Google and SO hasn't turned up anything that seems to apply. I can supply any additional code that might be relevant.
Thanks for looking! All help much appreciated!
Fixed, thanks to @rdelmar:
I replaced the if statement in the above code with:
if (([thisActivity.startTime timeIntervalSinceDate:self.detailStartDate] < 0) && ([thisActivity.stopTime timeIntervalSinceDate:self.detailStartDate] > 0))
Upvotes: 0
Views: 91
Reputation: 104092
You can't compare dates with "<", that is actually comparing the pointers to those dates, not the dates themselves. You can either use one of the date comparison methods listed in the NSDate Class reference, or you can convert the dates to a primitive with timeIntervalSince1970 (or one of the other timeIntervalSince... methods), and then use "<".
if (([thisActivity.startTime timeIntervalSince1970] < [self.detailStartDate timeIntervalSince1970] ) && ([thisActivity.stopTime timeIntervalSince1970] > [self.detailStartDate timeIntervalSince1970]))
Upvotes: 1