zoukankan      html  css  js  c++  java
  • ios中自定义checkbox

    //自定义button
    #import <UIKit/UIKit.h> @interface CKButton : UIButton @end #import "CKButton.h" #define KTitleWidth 0.6 #define KPadding 0.1 #define KImageWidth (1-KTitleWidth -2*KPadding) @implementation CKButton - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code //设置image self.adjustsImageWhenDisabled=NO; self.adjustsImageWhenHighlighted=NO; self.imageView.contentMode=UIViewContentModeScaleAspectFit; //设置title [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; } return self; } -(CGRect)imageRectForContentRect:(CGRect)contentRect{ CGFloat width=contentRect.size.width; CGFloat height=contentRect.size.height; return CGRectMake(0,0 , width*KImageWidth, height); } -(CGRect)titleRectForContentRect:(CGRect)contentRect{ CGFloat width=contentRect.size.width; CGFloat height=contentRect.size.height; return CGRectMake(width*(KImageWidth+KPadding), 0, width*KTitleWidth, height); } @end

    自定义cell

    #import "CkCell.h"
    #define KMinTag 10
    
    @interface CkCell ()
    {
        CKButton *_current;
    }
    @end
    
    @implementation CkCell
    
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            
            for (int i=0; i<KCount; i++) {
                CKButton *button=[[CKButton alloc] initWithFrame:CGRectZero];
                button.tag=KMinTag+i;
                [self.contentView addSubview:button];
                [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
            }
        }
        return self;
    }
    
    -(void)setDatacell:(NSArray *)datacell{
        if (_datacell!=datacell) {
            [_datacell release];
            _datacell=[datacell retain];
            int count=self.datacell.count;
            for (int i=0; i<KCount; i++) {
                CKButton *button=(CKButton *)[self.contentView viewWithTag:(KMinTag+i)];
                if ((i<count)) {
                    button.hidden=NO;
                    NSString *temp=self.datacell[i];
                    [button setTitle:temp forState:UIControlStateNormal];
                    [button setImage:[UIImage imageNamed:@"radio_normal"] forState:UIControlStateNormal];
                    [button setImage:[UIImage imageNamed:@"radio_selected"] forState:UIControlStateSelected];
                }
                else{
                    button.hidden=YES;
                }
            }
        }
    }
    
    -(void)layoutSubviews{
        [super layoutSubviews];
        int width=self.contentView.bounds.size.width/KCount;
        for (UIView *child in self.contentView.subviews) {
            if ([child isKindOfClass:[UIButton class]]) {
                int tag=child.tag-KMinTag;
                if (tag>=0 && tag<KCount) {
                    child.frame=CGRectMake(tag*width, 0, width, KHeight);
                }
            }
        }
    }
    
    
    -(void)click:(CKButton *)btn{
    
        btn.selected=!btn.selected;
       
        if (self.delegate ||[self.delegate respondsToSelector:@selector(CKCellCLick:)]) {
            [self.delegate CKCellCLick:btn];
        }
    }
    
    - (void)dealloc
    {
       
        [_datacell release];
        [super dealloc];
    }
    @end

    封装tableview

    #import <UIKit/UIKit.h>
    #import "CkCell.h"
    
    @interface CKTableView : UIView<UITableViewDataSource,UITableViewDelegate>
    @property(nonatomic,retain)NSArray *data;
    @property(nonatomic,assign)id ckDelegate;
    @end
    
    #import "CKTableView.h"
    
    @implementation CKTableView
    
    - (id)initWithFrame:(CGRect)frame 
    {
        self = [super initWithFrame:frame];
        if (self) {
            UITableView *tableview=[[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
            tableview.delegate=self;
            tableview.dataSource=self;
            tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
            [self addSubview:tableview];
            [tableview release];
        }
        return self;
    }
    
    
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 1;
    }
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        
        int count=self.data.count/KCount;
        if (!self.data.count%KCount==0) {
            count++;
        }
        return count;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *cellIdentify=@"CKTableView";
        CkCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentify];
        if (cell==nil) {
            cell=[[[CkCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify] autorelease] ;//注意一定要在这里加aurorelease,否则会报错,原因不明
        }
        cell.delegate=self.ckDelegate;
    
        int location=indexPath.row*KCount;
        int length=KCount;
        if (location+length>self.data.count) {
            length=self.data.count-location;
        }
        cell.datacell=[self.data subarrayWithRange:NSMakeRange(location, length)];
        
        return cell ;
    }
    
    - (void)dealloc
    {
        [_data release];
        [super dealloc];
    }
    
    @end

    viewcontroller中用法

    #import <UIKit/UIKit.h>
    #import "CKTableView.h"
    
    @interface CkViewController : UIViewController<CKCellDelegate>
    
    @property(nonatomic,retain)NSMutableArray *selectArrary;
    @end
    
    
    #import "CkViewController.h"
    
    
    @interface CkViewController ()
    
    @end
    
    @implementation CkViewController
    
    
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.view.backgroundColor=[UIColor whiteColor];
        self.selectArrary=[NSMutableArray array];
        NSArray *mydata=@[@"税收优惠",@"办税指南",@"最新政策",@"政策解读",@"热点问题",@"涉税通告",@"地税新闻"];
        for (int i=0; i<mydata.count; i++) {
            [self.selectArrary addObject:@0];
        }
        // Do any additional setup after loading the view.
        CKTableView *tableview=[[CKTableView alloc] initWithFrame:CGRectMake(10, 10, 300, 200)];
        tableview.ckDelegate=self;
        tableview.data=mydata;
        [self.view addSubview:tableview];
        [tableview release];
    }
    
    
    -(void)CKCellCLick:(CKButton *)btn{
        NSLog(@"__>%@",btn.titleLabel.text);
    }
    
    @end
  • 相关阅读:
    Oracle DBMS_PROFILER 的使用方式 (转)
    2010-09-01 22:29 oracle建表、建主键、外键基本语法
    Oracle11g对AWR的扩展
    Oracle中各个命中率的总结及调优笔记整理
    如何用 SQL Tuning Advisor (STA) 优化SQL语句
    aix分配硬盘
    AIX tar命令
    linux gzip命令参数及用法详解--linux压缩gzip文件命令
    AIX6.1下配置Nmon性能工具
    qqq
  • 原文地址:https://www.cnblogs.com/gcb999/p/3242415.html
Copyright © 2011-2022 走看看