How to Add Check Box in custom Table View Cell?

﹥>﹥吖頭↗ 提交于 2019-12-29 09:37:20

问题


I want to create custom check box in my Custom UITableViewCell. I have used following way in UITableViewCell but doesn't find it appropriate. I need to it inside from 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;

        }



    }
}

I want to do it as below using Tag

- (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:


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

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


来源:https://stackoverflow.com/questions/31158404/how-to-add-check-box-in-custom-table-view-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!