以下代码不会循环:
-(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 loop
和快速枚举。都不执行。如NSLogs
所示,无论采用哪种方法,我都输入方法,但跳过循环。我很确定自己写的代码不错,因为我在同一应用程序的许多其他地方都成功使用了非常相似的语法(两种类型)。是的,我研究了数十个关于SO的类似问题,还查阅了关于Objective C
for
循环的基本文章,但没有解决方案。和往常一样,我很可能犯了一个愚蠢的错误,但是,如果是这样,那就让我难以捉摸。
for
循环内的代码(为了响应关联的UILabel
的滚动,我试图重新定位UIView
)可能无关紧要,但是为了以防万一,我将其包括在内。所有帮助和意见表示赞赏!
编辑:
如下所述,此问题的答案是相关的dayBarArray为空。我只是检查了添加对象的代码,并感到困惑。我当然应该已经在我的NSLogs中看到了明显的答案,并且没有任何借口,除了我之前已经检查了添加代码并且栏似乎已添加。当我弄清楚那里有什么问题时,我会报告。
第二编辑:
好的,这对我来说是一个愚蠢的错误-我尚未初始化数组。一样,如果我没有发布问题,并且由于我相信我已经检查了数组的内容(实际上,我正在检查是否已将标签应用于UIViews),我不知道它将持续多长时间带我意识到原木在告诉我什么。感谢所有回答!
最佳答案
dayBarArray.count
为零,循环中的条件为i < dayBarArray.count
。在进入循环主体之前,它会测试0 < 0
,这是错误的,因此它永远不会进入主体。
关于ios - For循环不会执行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25352105/