2012-03-21 69 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 
} 
相关问题