• CheckBox UITableViewCell


    一个简单的自定义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
  • 相关阅读:
    P3275 [SCOI2011]糖果 题解
    hdu 2962 题解
    hdu 2167 题解
    hdu 2476 题解
    hdu 5418 题解
    2019.10.16&17小结
    poj 3061 题解(尺取法|二分
    poj 1852&3684 题解
    NOIP2017[提高组] 宝藏 题解
    一类经典问题的解法
  • 原文地址:https://www.cnblogs.com/ishawn/p/TableView.html
Copyright © 2020-2023  润新知