如何在自定义表视图单元格中添加复选框?

时间:2015-07-01 10:09:47

标签: ios objective-c uitableview tags

我想在Custom UITableViewCell中创建自定义复选框。我在UITableViewCell中使用了以下方式,但没有找到合适的方法。我需要在cellForRowAtIndexPath

里面
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

 }

- (IBAction)ActionBtnSelect:(id)sender {

    if ([[UIImage imageNamed:@"checkbox.png"] isEqual:BtnCheckBox.currentImage] || [[UIImage imageNamed:@"uncheckbox.png"] isEqual:BtnCheckBox.currentImage]) {
        // do something


        if (!bool_Select) {

            [BtnCheckBox setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
            bool_Select =YES;

        }
        else  if (bool_Select) {
            //NSLog(@"check box untick");

            [BtnCheckBox setImage:[UIImage imageNamed:@"uncheckbox.png"] forState:UIControlStateNormal];
            bool_Select =NO;

        }

    }else{

        if (!bool_Select) {

            [BtnCheckBox setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
            bool_Select =YES;

        }
        else  if (bool_Select) {
            //NSLog(@"check box untick");

            [BtnCheckBox setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
            bool_Select =NO;

        }



    }
}

enter image description here

我想按照使用标记

进行操作
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{---------------------------------

cell.BtnCheckBox.tag = indexPath.row;
[cell.BtnCheckBox addTarget:self action:@selector(ActionSingleSelect:) forControlEvents:UIControlEventTouchUpInside];

}

-(void)ActionSingleSelect:(UIButton*)button
{
}

1 个答案:

答案 0 :(得分:0)

cell.btn = [[UIButton alloc]init];

cell.btn.tag=indexPath.row;
[cell.btn addTarget:self action:@selector(ActionSingleSelect:) forControlEvents:UIControlEventTouchUpInside];