【发布时间】:2011-05-16 11:05:47
【问题描述】:
嗨,
在我的应用程序中,我在 scrollView 中显示一个列表。我已经针对每个项目放置了自定义 btn 在滚动视图中。在 btn 操作中,我必须将未选择的图像更改为选定的图像。但是当我点击第一个 btn 时,最后一个 btn 图像被更改。
代码:
- (void)reLoadView
{
float yCoordinateStartLocation = 20;
float xCoordinateStartLocatioForItem = 80;
float xCoordinateStartLocatioForButton=30;
float questionWidth =100;
UIFont* questionFont = [UIFont boldSystemFontOfSize:19];
UIColor *color = [UIColor colorWithRed:0.0 green:54.0/255.0 blue: 129.0/255.0 alpha:1.0];
UIColor *bgcolor = [UIColor clearColor];
for(int i=0; i<[itemArray count]; i++)
{
showItems *showItem = [itemArray objectAtIndex:i];
float itemHeight = [ShowViewController calculateHeightOfTextFromWidth:showItem.itemName :questionFont : questionWidth :UILineBreakModeCharacterWrap];
itemLabel=[[UILabel alloc]init];
itemLabel.frame=CGRectMake(xCoordinateStartLocatioForItem,yCoordinateStartLocation,questionWidth,40);
itemLabel.text =showItem.itemName;
itemLabel.textColor = color;
itemLabel.font = questionFont;
itemLabel.numberOfLines = 0;
itemLabel.lineBreakMode = UILineBreakModeCharacterWrap;
itemLabel.textAlignment=UITextAlignmentLeft;
itemLabel.backgroundColor = bgcolor;
[scrollView addSubview:itemLabel];
UIImage *unSelectedImage = [UIImage imageNamed:@"unChecked.png"];
checkBoxBttn = [[MyCustomButton alloc] initWithIdValue:showItem.itemName tagValue:tagCount];
//checkBoxBttn = [[MyCustomButton alloc] initWithIdValue:showItem.itemName];
CGRect frame = CGRectMake(xCoordinateStartLocatioForButton,yCoordinateStartLocation, 30, 30);
[checkBoxBttn setFrame:frame];
checkBoxBttn.tag=tagCount;
[checkBoxBttn setBackgroundImage:unSelectedImage forState:UIControlStateNormal];
[checkBoxBttn setShowsTouchWhenHighlighted:YES];
[checkBoxBttn addTarget:self action:@selector(checkButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:checkBoxBttn];
tagCount++;
yCoordinateStartLocation = yCoordinateStartLocation + itemHeight + 20;
}
scrollView.contentSize = CGSizeMake(320, yCoordinateStartLocation+50);
}
-(void)checkButtonAction:(MyCustomButton*)sender
{
NSString *sndStr = [sender itemName];
//taglabel=[sender tagNumber];
//NSString *tagNo=[NSString stringWithFormat:@"%d",taglabel];
for(int i=0; i<[itemArray count]; i++)
{
showItems *showItem = [itemArray objectAtIndex:i];
if([sndStr isEqualToString:showItem.itemName])
{
printf("\n ==== checkBoxBttn.tag:%d", checkBoxBttn.tag);
UIImage *selectedImage = [UIImage imageNamed:@"checked.png"];
[checkBoxBttn setBackgroundImage:selectedImage forState:UIControlStateNormal];
}
}
}
任何人都可以帮助我。 提前致谢。
【问题讨论】:
-
你在代码中哪里定义了 checkBoxBttn?
标签: iphone objective-c