//自定义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(, , width*KImageWidth, height); } -(CGRect)titleRectForContentRect:(CGRect)contentRect{
CGFloat width=contentRect.size.width;
CGFloat height=contentRect.size.height;
return CGRectMake(width*(KImageWidth+KPadding), , 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=; 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=; 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>= && tag<KCount) {
child.frame=CGRectMake(tag*width, , 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 ;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ int count=self.data.count/KCount;
if (!self.data.count%KCount==) {
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=; i<mydata.count; i++) {
[self.selectArrary addObject:@];
}
// Do any additional setup after loading the view.
CKTableView *tableview=[[CKTableView alloc] initWithFrame:CGRectMake(, , , )];
tableview.ckDelegate=self;
tableview.data=mydata;
[self.view addSubview:tableview];
[tableview release];
} -(void)CKCellCLick:(CKButton *)btn{
NSLog(@"__>%@",btn.titleLabel.text);
} @end

ios中自定义checkbox的更多相关文章

  1. [转]Android中自定义checkbox样式

    android中自定义checkbox的图片和大小   其实很简单,分三步: 1.在drawable中创建文件checkbox_selector.xml: <?xml version=" ...

  2. ios中自定义tableView,CollectionView的cell什么时候用nib加载,什么时候用标识重用

    做了一段时间的iOS,在菜鸟的路上还有很长的路要走,把遇到的问题记下来,好记性不如烂笔头. 在项目开发中大家经常会用到tableView和collectionView两个控件,然而在cell的自定义上 ...

  3. ios中自定义cell 设置cell的分组结构

    ios系统默认的cell并不能满足我们的需求 这个时候就需要自定义我们的cell 自定义cell为分组的时候 需要设置分组样式  以下是我常用分组的二种方法: 第一是 在自定义的UITableView ...

  4. iOS中 自定义cell分割线/分割线偏移 韩俊强的博客

    在项目开发中我们会常常遇到tableView 的cell分割线显示不全,左边会空出一截像素,更有甚者想改变系统的分割线,并且只要上下分割线的一个等等需求,今天重点解决以上需求,仅供参考: 每日更新关注 ...

  5. IOS开发自定义CheckBox控件

    IOS本身没有系统的CheckBox组件,但是实际开发中会经常用到,所以专门写了一个CheckBox控件,直接上代码 效果图: UICheckBoxButton.h文件如下: #import #imp ...

  6. Android中自定义Checkbox

    custom_checkbox.xml文件: <?xml version="1.0" encoding="utf-8"?> <selector ...

  7. ios中自定义图层的2种方法

    1:自定义图层,在图层中画图 #import <QuartzCore/QuartzCore.h> @interface MJLayer : CALayer @end #import &qu ...

  8. ios中自定义图层

    图层和VIEW的区别 1:view不具备显示功能,是因view内部有一个图层,才能显示出来 2:图层不具备事件功能,VIEW继承UIRespone具有处理事件功能 3:自定义的图层有一个影式动画,VI ...

  9. android中自定义checkbox的图片和大小

    其实很简单,分三步: 1.在drawable中创建文件checkbox_selector.xml: <?xml version="1.0" encoding="ut ...

随机推荐

  1. asp.net 获得域名,端口,虚拟目录[转]

    asp.net 获得域名,端口,虚拟目录 记性不好,好多次都被路径问题给拦住了.我现在想得到一个资源的全URL路径,因此首先想得到网站当前的域名,端口和虚拟目录.看下表 底下這張表就是各種跟 Brow ...

  2. 2018.08.28 ali 梯度下降法实现最小二乘

    - 要理解梯度下降和牛顿迭代法的区别 #include<stdio.h> // 1. 线性多维函数原型是 y = f(x1,x2,x3) = a * x1 + b * x2 + c * x ...

  3. easyloader分析与使用

    转载自:http://www.cnblogs.com/haogj/archive/2013/04/22/3036685.html 使用脚本库总要加载一大堆的样式表和脚本文件,在 easyui 中,除了 ...

  4. IOS 多线程 NSThread

    一个正在运行的应用程序是一个进程,一个进程会默认开启一个主线程,但是在主线程中的操作是串行的,也就是当有多个任务同时需要完成的时候,是按照顺序一个个执行.因此,为了提高效率,会在进程中开启多个线程,每 ...

  5. 从头认识java-18.2 主要的线程机制(4)-优先级

    这一章节我们来讨论一下多线程的优先级问题. 1.样例: package com.ray.ch17; public class Test { public static void main(String ...

  6. 解决 win7 注册com组件失败问题

    解决 win7 注册com组件失败问题 运行:regsvr32 xxx.ocx 提示:模块 "xxx.ocx" 已加载,但对调用 dllregisterserver 的调用失败,错 ...

  7. 集成学习总结 & Stacking方法详解

    http://blog.csdn.net/willduan1/article/details/73618677 集成学习主要分为 bagging, boosting 和 stacking方法.本文主要 ...

  8. 机器学习算法与Python实践之(七)逻辑回归(Logistic Regression)

    http://blog.csdn.net/zouxy09/article/details/20319673 机器学习算法与Python实践之(七)逻辑回归(Logistic Regression) z ...

  9. Python在VSCode中进入交互界面调试

    VSCode非常强大,断点好用,美中不足,每次只能通过下面窄窄一行进行各种检查,而python的优点就在于交互式的调试,所以希望能够在断点时能够进入到正常的交互界面进行调试. 我用的插件是: 设置交互 ...

  10. GPUImage API文档之GPUImageFramebufferCache类

    GPUImageFramebufferCache类负责管理GPUImageFramebuffer对象,是一个GPUImageFramebuffer对象的缓存. 方法 - (GPUImageFrameb ...