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 :(得分:2)
您可以自定义UITableViewCell
以获得一个按钮,点击该按钮会执行必要的操作
<强>更新强>
-(void)starClicked:(id)sender
{
UIButton *button = (UIButton *)sender;
NSLog(@"btnID:%d",button.tag);
if(button.tag == initialTag) {
[button setImage:[UIImage imageNamed:@"star.png" forState:UIControlStateNormal]];
button.tag = newTag;
}
else if(button.tag == newTag)
{
[button setImage:[UIImage imageNamed:@"dot.png" forState:UIControlStateNormal]];
button.tag = initialTag;
}
NSLog(@"btnID:%d",btn.tag);
}
另一个选项
-(void)starClicked:(id)sender
{
UIButton *button = (UIButton *)sender;
button.selected = !button.selected; // toggle the selected property,
if (button.selected)
{
// code to change image for selected state.
}
else
{
// code to change image for normal state.
}
}
答案 1 :(得分:0)
您可以将UIButton
添加到表格单元格中,这会在用户按下时切换。
-(void) toggleStarButton:(id)sender
{
if([sender tag]== 0)
{
[sender setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
[sender setTag:1];
else if([sender tag]==1)
{
[sender setImage:[UIImage imageNamed:@"unStar.png"] forState:UIControlStateNormal];
[sender setTag:0];
}
}