以下代码不循环:
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"Inside method, before for loop");
NSLog(@"dayBarArray.count = %lu", (unsigned long)dayBarArray.count);
for (int i = 0; i < dayBarArray.count; i++) //*** I've tried it this way
// for (DayChartBar *bar in dayBarArray) //*** I've tried it this way
{
NSLog(@"Inside for loop");
DayChartBar *bar = [dayBarArray objectAtIndex:i];
UIView *thisLabel = [self.scroller viewWithTag:(bar.tag + 100)];
CGRect visibleRect;
visibleRect.origin = self.scroller.frame.origin;
visibleRect.size = self.scroller.bounds.size;
NSLog(@"bar.frame = %@",NSStringFromCGRect(bar.frame));
NSLog(@"visibleRect.frame = %@",NSStringFromCGRect(visibleRect));
if (CGRectIntersectsRect(visibleRect,bar.frame))
{
NSLog(@"Inside if statement");
CGRect intersection = CGRectIntersection(visibleRect,bar.frame);
NSLog(@"bar.Frame is %@",NSStringFromCGRect(bar.frame));
thisLabel.center = CGPointMake((thisLabel.center.x), CGRectGetMidY(intersection));
}
else
{
thisLabel.center = thisLabel.center;
NSLog(@"Inside else statement");
}
}
NSLog(@"Still inside method, after for loop");
}
以下是策略性放置的 NSLog
语句的结果:
2014-08-17 10:07:37.221 WMDGx[54442:90b] Inside method, before for loop
2014-08-17 10:07:37.222 WMDGx[54442:90b] dayBarArray.count = 0
2014-08-17 10:07:37.223 WMDGx[54442:90b] Still inside method, after for loop
如您所见,我已经尝试了传统的for 循环
和快速枚举。也不会执行。正如 NSLogs
所示,我输入了方法,但跳过了循环,不管我采用了哪种方法。
我很确定我写了很好的代码,因为我已经在同一个应用程序的许多其他地方成功地使用了非常相似的语法(两种类型)。是的,我已经查看了数十个关于 SO 的类似问题,并查阅了有关 Objective C for
循环的基本文本,但没有解决方案。
一如既往,我对自己犯了一个愚蠢错误的可能性持开放态度,但如果是这样,我就无法接受。 for
循环中的代码(我正在尝试重新定位 UILabel
以响应关联的 UIView
的滚动)可能是无关紧要的,但我把它包括在内以防万一。
感谢所有帮助和意见!
编辑:
正如下面所指出的,这个问题的答案是相关的 dayBarArray 是空的。我刚刚检查了添加对象的代码,并且感到困惑。我当然应该在我的 NSLogs 中看到明显的答案,并且没有任何借口,除了我之前检查过添加代码并且似乎添加了栏。当我弄清楚那里有什么问题时,我会报告回来。
第二次编辑:
好吧,这是我犯的一个愚蠢的错误——我没有初始化数组。同样,如果我没有发布问题,并且因为我相信我已经检查了数组的内容(实际上,我正在检查标签是否已应用于 UIView),我不知道它会持续多久让我意识到日志告诉我的是什么。感谢所有回复的人!
最佳答案
dayBarArray.count
为零,循环条件为i < dayBarArray.count
.在进入循环体之前,它测试 0 < 0
,这是错误的,因此它永远不会进入人体。
关于ios - for循环不会执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25352105/