开发者

Objective-C - Loop played but loop condition is false

I'm trying to convert a sectionned table into a flat list using this function into didSelectRowAtIndexPath (I have a NSArray that is initialisated with the number of items contained in each section) :

Somewhere... :-)

self.sectionsArray = [NSArray arrayWithObjects:macroIntNumber(1), macroIntNum开发者_如何学Pythonber(3), macroIntNumber(12), nil];

then into didSelectRowAtIndexPath :

 int selectedRow = 0;
 int a = indexPath.section;

 for (int i=0; i<indexPath.section-1; i++) {
  selectedRow += [[self.sectionsArray objectAtIndex:i] intValue];
 }
 selectedRow += indexPath.row;

But... This crashes for indexPath.section = 0 (first section). Because the loop is played infinitly until crash of the NSArray call... Strange !!!

forcing for (int i=0; i<0-1; i++) { works

forcing for (int i=0; i<a-1; i++) { works

What am I missing ?


section is an NSUInteger, so it's unsigned. Thus, subtracting 1 from 0 on that unsigned integer is taking you to a very large number rather than -1.

It works when using a, because you've declared a as an int. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜