iPhone中的表视图中的复选框按钮

时间:2010-12-27 05:41:47

标签: iphone checkbox

我想在tableview的每一行中创建一个复选框。我的代码只适用于一个复选框。如果我有超过1则失败。我的代码是

  UIButton   * checkBox=[UIButton buttonWithType:UIButtonTypeCustom];
        checkBox.tag=indexPath.row;
        checkBox.frame=CGRectMake(270,15, 20, 20);
        [cell.contentView addSubview:checkBox];
        [checkBox setImage:[UIImage imageNamed:@"selectedBoxWith.png"] forState:UIControlStateNormal];
        [checkBox addTarget:self action:@selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];

-(void)checkBoxClicked:(id)sender{
if(self.isChecked ==NO){
  self.isChecked =YES;
  [sender  setImage:[UIImage imageNamed: @"selectedBoxWithTik.png"] forState:UIControlStateNormal];
}else
{
   self.isChecked =NO;
  [sender setImage:[UIImage imageNamed:@"selectedBoxWith.png"]forState:UIControlStateNormal];

    }
}

如何处理更多复选框?

3 个答案:

答案 0 :(得分:3)

你只需要尝试这个。它会起作用..........

-(void)checkBoxClicked:(id)sender{

    UIButton *tappedButton = (UIButton*)sender;

    if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"selectedBoxWith.png"]]) {
        [sender  setImage:[UIImage imageNamed: @"selectedBoxWithTik.png"] forState:UIControlStateNormal];
    }

    else {
        [sender setImage:[UIImage imageNamed:@"selectedBoxWith.png"]forState:UIControlStateNormal];
    }
}

答案 1 :(得分:1)

您不能简单地使用简单的UIButton来执行此操作,因为您需要检测按下了哪个行号复选框。因此,而不是使用UIButton直接使用扩展UIButton的CustomButton并且具有额外的属性rowTag,如:

@interface CustomButton : UIButton {
  NSInteger rowTag;
}

@property NSInteger rowTag;

@end

@implementation CustomButton

@synthesize rowTag;

@end

现在在cellForRowAtIndexPath

if(cell==nil){
        CustomButton   * checkBox=[CustomButton buttonWithType:UIButtonTypeCustom];
        checkBox.tag=21;
        checkBox.frame=CGRectMake(270,15, 20, 20);
        [cell.contentView addSubview:checkBox];
        [checkBox addTarget:self action:@selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
}
    CustomButton *btn = [cell.contentView viewWithTag:21];
    [btn setRowTag:indexPath.row];
    if(selectedProperty){
       [checkBox setImage:[UIImage imageNamed:@"selectedBoxWithTik.png"] forState:UIControlStateNormal];
    }
    else
       [checkBox setImage:[UIImage imageNamed:@"selectedBoxWith.png"] forState:UIControlStateNormal];

希望这有帮助。

答案 2 :(得分:0)

示例,单击第一行用于更改其他行。单击其他行仅更改自身。 arrCheckbox是一个用于存储所有行检查的数组。

- (UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSLog(@"AV--%s", __func__);
    HistoryCell2 *cell = [tableview dequeueReusableCellWithIdentifier:CellId];

    if (cell == nil) {
        NSLog(@"Create new row");
        [tableview registerNib:[UINib nibWithNibName:@"HistoryCell2" bundle:nil] forCellReuseIdentifier:CellId];
        cell = [tableview dequeueReusableCellWithIdentifier:CellId];
    }
    cell.tag = indexPath.row;

    BOOL checked =  [[arrCheckbox objectAtIndex:indexPath.row] boolValue];
    UIImage *image = (checked) ? [UIImage imageNamed:@"cb_on.png"] : [UIImage imageNamed:@"cb_off.png"];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(1.0, 1.0, 29, 29);
    button.frame = frame;   // match the button's size with the image size
    button.tag = indexPath.row;
    [button setBackgroundImage:image forState:UIControlStateNormal];

    // set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
    [button addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:button];

    return cell;
}

- (void) checkButtonTapped:(id)sender
{
    NSLog(@"%s", __func__);
    UIButton *tappedButton = (UIButton*)sender;
    int index = (int)tappedButton.tag;
    NSLog(@"Row button: %d", index);

    if(index > 0)
    {
        BOOL checked = [[arrCheckbox objectAtIndex:index] boolValue];
        [arrCheckbox removeObjectAtIndex:index];
        [arrCheckbox insertObject:(checked) ? @"0":@"1" atIndex:index];

        UIImage *newImage = (checked) ? [UIImage imageNamed:@"cb_off.png"] : [UIImage imageNamed:@"cb_on.png"];
        [tappedButton setBackgroundImage:newImage forState:UIControlStateNormal];
    }
    else{
        //UITableView *tableview = (UITableView *)[[tappedButton superview] superview];
        UIImage *newImage;
        BOOL checked = [[arrCheckbox objectAtIndex:0] boolValue];
        for(int i=0; i<[arrCheckbox count]; i++)
        {
            //NSLog(@"Row: %d------", i);
            [arrCheckbox removeObjectAtIndex:i];
            [arrCheckbox insertObject:(checked) ? @"0":@"1" atIndex:i];

            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
            for(UIView *subview in cell.contentView.subviews)
            {
                if([subview isKindOfClass: [UIButton class]])
                {
                    //NSLog(@"Modify button");
                    tappedButton = (UIButton*)subview;
                    //[subview removeFromSuperview];
                    newImage = (checked) ? [UIImage imageNamed:@"cb_off.png"] : [UIImage imageNamed:@"cb_on.png"];
                    [tappedButton setBackgroundImage:newImage forState:UIControlStateNormal];
                }
            }
        }
    }
}