2012-03-21 44 views
0

我是從一些字符串生成列表至多4cocos2d的組相同的字符串

NSUInteger total = MIN(4, myNSMutableArray.count); 
    CGFloat xPos = 0.0f; 
    CGFloat yPos = 450.0f; 
    CGFloat yPadding = 10.0f; 
    NSInteger count = 0; 
    for (ItemClass *item in myNSMutableArray) { 
     if (count == total) return; 

     CCLabelTTF *itemLabel = [CCLabelTTF labelWithString:item.name 
               dimensions: CGSizeMake(280, 50) 
                alignment:UITextAlignmentCenter 
               lineBreakMode:UILineBreakModeWordWrap 
                fontName:@"MyFont.otf" 
                fontSize:28.0f]; 
     itemLabel.anchorPoint = ccp(0,0); 
     itemLabel.position = ccp(xPos, yPos); 
     itemLabel.tag = item.itemID; 
     [self addChild:itemLabel]; 
     [myNSMutableArrayOfLabels addObject:itemLabel]; 
     yPos -= itemLabel.contentSize.height + yPadding; 
     count++; 
    } 

讓我們留我有像字符串列表:串1,串2,串3,串3,串3 ,串3.我想將它們分組,以獲得類似:

string 1 
string 2 
string 3 (4) 
+0

'myArray'的類型是什麼? NSArray,CCArray還是什麼? – 2012-03-21 15:51:21

+0

@ RichardJ.RossIII:'NSMutableArray',謝謝! – 2012-03-21 15:53:30

回答

2

因爲你的數據是NSMutableArray的,這是很簡單的:

NSArray *sorted = [myArray sortedArrayUsingComparator:^(id obj1, id obj2) { 
    assert([obj1 isKindOfClass:[ItemClass class]]); 
    assert([obj2 isKindOfClass:[ItemClass class]]); 

    ItemClass *item1 = obj1; 
    ItemClass *item2 = obj2; 

    return [item1.name compare:item2.name]; 
}]; 

for (ItemClass *item in sorted) 
{ 
    // create your labels 
} 
相關問題