uitableviewcell选中打勾

单元格选中后,打勾。再选其他,之前的勾取消,新的打勾。

代码如下(不加代码高亮了吧):

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSIndexPath *oldIndex = [tableView indexPathForSelectedRow];

[tableView cellForRowAtIndexPath:oldIndex].accessoryType = UITableViewCellAccessoryNone;

[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;

return indexPath;

}

添加按钮后设置选中和或不选中的图片(需要实现自定义的uitableViewcell)

-(void)checkboxClick:(UIButton *)btn
{
    btn.selected = !btn.selected;
}


- (void)viewDidLoad {
UIButton *checkbox = [UIButton buttonWithType:UIButtonTypeCustom];
    
    CGRect checkboxRect = CGRectMake(135,150,36,36);
    [checkbox setFrame:checkboxRect];
    
    [checkbox setImage:[UIImage imageNamed:@"checkbox_off.png"] forState:UIControlStateNormal];
    [checkbox setImage:[UIImage imageNamed:@"checkbox_on.png"] forState:UIControlStateSelected];
    
    [checkbox addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:checkbox];    
}

posted @ 2016-07-20 09:43  樊特西style  阅读(2986)  评论(0编辑  收藏  举报