自定义uitableviewcell中看似随机的标签文本更改(Seemingly random label text changes in custom uitableviewcell)

编程入门 行业动态 更新时间:2024-10-23 07:27:13
自定义uitableviewcell中看似随机的标签文本更改(Seemingly random label text changes in custom uitableviewcell)

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

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

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

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

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

在这个镜头中,您可以看到其他单元格中的标签读作“STARTED EARLIER”,即使它不应该。

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

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

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

最满意答案

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

if (([thisActivity.startTime timeIntervalSince1970] < [self.detailStartDate timeIntervalSince1970] ) && ([thisActivity.stopTime timeIntervalSince1970] > [self.detailStartDate timeIntervalSince1970]))

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

更多推荐

本文发布于:2023-08-03 13:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1389304.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   文本   标签   uitableviewcell   Seemingly

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!