如何通过button.tag从数据库中将图像分配给按钮

时间:2011-03-23 04:46:25

标签: iphone objective-c uiimage

我在根据数据库中的整数值更改按钮图像时遇到问题。 1用于星形图像0用于点图像

以下是相关代码:

这个if和else循环在cellforrowatindexpath

if ([stararray count] > 0) {
    for (int i = 0;i < [stararray count]; i++) {
        NSString *starstr =[stararray objectAtIndex:i];
        NSInteger starinteger = [starstr integerValue];
        btn = [[UIButton alloc] initWithFrame:kLabelRect2];
        btn.tag =starinteger - 1;
        NSLog(@"btnID1:%d",btn.tag);
        [btn setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(starClicked:) 
        forControlEvents:UIControlEventTouchUpInside];

        [cell.contentView addSubview:btn];
        btn.hidden = inPseudoEditMode;
    }

    for (int i = 0;i < [nostararray count]; i++) {
        NSString *starstr =[nostararray objectAtIndex:i];
        NSInteger starinteger = [starstr integerValue];
        btn = [[UIButton alloc] initWithFrame:kLabelRect2];
        btn.tag =starinteger - 1;
        NSLog(@"btnID2:%d",btn.tag);
        [btn setImage:[UIImage imageNamed:@"dot.png"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(starClicked:)    forControlEvents:UIControlEventTouchUpInside];
                                [cell.contentView addSubview:btn];
        btn.hidden = inPseudoEditMode;
    }

    //stararray = [[NSMutableArray alloc]init];
}
else {
    btn = [[UIButton alloc] initWithFrame:kLabelRect2];
    btn.tag =indexPath.row;
    NSLog(@"btnID2:%d",btn.tag);
    [btn setImage:[UIImage imageNamed:@"dot.png"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(starClicked:) 
    forControlEvents:UIControlEventTouchUpInside];

    [cell.contentView addSubview:btn];
    btn.hidden = inPseudoEditMode;
}

0 个答案:

没有答案