自定义uitableviewcell中看似随机的标签文本更改

时间:2014-06-05 05:13:38

标签: ios uitableview uilabel

我有UITableView根据标签内的多个因素显示动态信息。标签又是用作可重用单元格的自定义UITableViewCell的属性。除了一个讨厌的标签self.thisCustomCell.fromDateLabel之外,一切似乎都能正常工作。

此标签应反映特定活动的定时间隔是否与所选搜索时间范围的开始时间重叠或不重叠。如果是,标签应显示为“STARTED EARLIER”。如果没有,标签应该读取相关活动的实际startTime。实际上,在一般情况下,按时间顺序排列的最古老(最底层)标签应总是说“开始更早”。

然而,无论是最旧的细胞还是其他细胞,这种标签的行为都是不稳定的。这意味着最老的单元格(其持续时间应始终与时间框架的起始关系重叠)通常会提供startTime,而其他单元格应该从不显示“STARTED EARLIER”,有时会这样做

以下是一些屏幕截图来说明问题:

顺序按时间顺序递减,第一张照片中最底部的单元格跨越时间帧开始时间,因此右下角标签应显示为“STARTED EARLIER”。

enter image description here

在这张照片中,您可以看到其他单元格中的标签是“STARTED EARLIER”,即使它不应该。

enter image description here

以下是我认为的相关代码:

-(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];
    }

}

有人可以指出我做错了什么吗?对Google和SO的广泛搜索没有发现任何似乎适用的内容。我可以提供任何可能相关的其他代码。

感谢您的期待!所有人都非常感谢!

已修复,感谢@rdelmar:

我用以下代码替换了上面代码中的if语句:

if (([thisActivity.startTime timeIntervalSinceDate:self.detailStartDate] < 0) && ([thisActivity.stopTime timeIntervalSinceDate:self.detailStartDate] > 0))

1 个答案:

答案 0 :(得分:1)

您无法将日期与&#34;&lt;&#34;进行比较,实际上是将指针与这些日期进行比较,而不是日期本身。您可以使用NSDate类引用中列出的日期比较方法之一,也可以使用timeIntervalSince1970(或其他timeIntervalSince ...方法之一)将日期转换为基元,然后使用&#34;&lt; &#34;

if (([thisActivity.startTime timeIntervalSince1970]  < [self.detailStartDate timeIntervalSince1970] ) && ([thisActivity.stopTime timeIntervalSince1970] > [self.detailStartDate timeIntervalSince1970]))
相关问题