0

我已经能够将 UITableView 添加到 cocos2d 层,它似乎可以滚动和处理委托操作,例如didSelectRowAtIndexPath.

但我想要的是在 tableview 单元格上放置精灵,自定义位图字体,基本上将每个 UITableViewcell 视为我可以使用 cocos2d 元素自定义的东西,但我不知道该怎么做。

// HelloWorldLayer implementation
@implementation HelloWorldLayer
@synthesize myTableView;

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    HelloWorldLayer *layer = [HelloWorldLayer node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init])) {

        self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, 300, 150) style:UITableViewStylePlain];
        // Set TableView Attributes
        self.myTableView.backgroundColor = [UIColor clearColor];
        self.myTableView.dataSource = self;
        self.myTableView.delegate = self;
        self.myTableView.opaque = YES;

        // Add View To Scene
        [[[CCDirector sharedDirector] openGLView] addSubview:self.myTableView];
    }
    return self;
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
    // in case you have something to dealloc, do it in this method
    // in this particular example nothing needs to be released.
    // cocos2d will automatically release all the children (Label)

    [self.myTableView release];
    self.myTableView = nil;

    // don't forget to call "super dealloc"
    [super dealloc];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"Header";
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    CCLabelTTF *lblNumber = [CCLabelTTF labelWithString:@"12345" fontName:@"Georgia" fontSize:14];
    [lblNumber setAnchorPoint:CGPointMake(0, 0.5)];

    CCSprite *spriteIcon = [CCSprite spriteWithFile:@"Icon.png"];

    // I want to add my the lblNumber and spriteIcon to the uitableviewcell


    return cell;
}


// - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath


-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"You selected index - %d", indexPath.row);
}


@end

因此,我的问题是——如何将精灵、自定义位图字体和其他 cocos2d 元素添加到 UITableViewCell?

谢谢

4

1 回答 1

0

如果你想做那种事情,你可能不应该使用 cocos2d。通过自定义 UITableViewCell 可以很容易地将图像和字体添加到 UITableViews 中。

于 2012-09-28T02:20:21.220 回答