简体   繁体   中英

Seemingly random label text changes in custom uitableviewcell

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))

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]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM