CheckBox UITableViewCell

本文介绍了一个简单的自定义UITableViewCell实现带有CheckBox的功能。通过继承UITableViewCell并添加一个UIButton作为CheckBox,实现了单元格选中状态的切换,并调整了按钮样式及位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个简单的自定义CheckBox UITableViewCell

效果:

UITableViewCheckBoxCell.h

 1 //
 2 //  UITableViewCheckBoxCell.h
 3 //  TableViewStudy
 4 //
 5 //  Created by Shawn on 12/16/12.
 6 //  Copyright (c) 2012. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 @interface UITableViewCheckBoxCell : UITableViewCell
12 
13 @property (nonatomic) BOOL isChecked;
14 
15 - (id)initWithReuseIdentifier:(NSString *)reuseIdentifier;
16 
17 @end

UITableViewCheckBoxCell.m

 1 //
 2 //  UITableViewCheckBoxCell.m
 3 //  TableViewStudy
 4 //
 5 //  Created by Shawn on 12/16/12.
 6 //  Copyright (c) 2012. All rights reserved.
 7 //
 8 
 9 #import "UITableViewCheckBoxCell.h"
10 @interface UITableViewCheckBoxCell()
11 
12 @property (nonatomic,retain) UIButton *btnCheck;
13 @end
14 
15 @implementation UITableViewCheckBoxCell
16 @synthesize btnCheck = _btnCheck;
17 @synthesize isChecked = _isChecked;
18 
19 - (void)dealloc
20 {
21     [_btnCheck release];
22     [super dealloc];
23 }
24 
25 - (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
26 {
27     self = [self initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
28     
29     return self;
30 }
31 
32 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
33 {
34     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
35     if (self) {
36         _isChecked = FALSE;
37         CGRect rect = self.contentView.bounds;
38         rect.origin.x = 10;
39         rect.origin.y = 5;
40         rect.size.width -= 45;
41         [self.textLabel setFrame:rect];
42         
43         rect = self.contentView.bounds;
44         rect.origin.x = rect.size.width - 35;
45         rect.origin.y = (rect.size.height - 25) / 2;
46         rect.size.width = 25;
47         rect.size.height = 25;
48         _btnCheck = [[UIButton alloc] initWithFrame:rect];
49         [_btnCheck setBackgroundColor:[UIColor lightGrayColor]];
50         [_btnCheck setTitle:@"" forState:UIControlStateNormal];
51         [_btnCheck setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
52         [_btnCheck.titleLabel setFont:[UIFont systemFontOfSize:12]];
53         
54         [_btnCheck addTarget:self action:@selector(btnCheckClicked) forControlEvents:UIControlEventTouchUpInside];
55         self.selectionStyle = UITableViewCellSelectionStyleNone;
56         [self.contentView addSubview:_btnCheck];
57     }
58     return self;
59 }
60 
61 - (void)btnCheckClicked
62 {
63     if (!self.isChecked) {
64         [_btnCheck setTitle:@"" forState:UIControlStateNormal];
65         [_btnCheck setBackgroundColor:[UIColor redColor]];
66         _btnCheck.selected = TRUE;
67         self.isChecked = TRUE;
68     }
69     else 
70     {
71         [_btnCheck setTitle:@"" forState:UIControlStateNormal];
72         [_btnCheck setBackgroundColor:[UIColor lightGrayColor]];
73         _btnCheck.selected = FALSE;
74         self.isChecked = FALSE;
75     }
76 }
77 - (void)setSelected:(BOOL)selected animated:(BOOL)animated
78 {
79     [super setSelected:selected animated:animated];
80     
81     // Configure the view for the selected state
82 }
83 
84 @end

 

转载于:https://www.cnblogs.com/ishawn/archive/2012/12/16/TableView.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值