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];    
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM