//使用tableview来进行布局checkBox.便于全选,全不选
//radiobutton 适合用RadioButton #import <UIKit/UIKit.h> @interface CheckView : UIView<UITableViewDataSource,UITableViewDelegate>
- (id)initWithFrame:(CGRect)frame Data:(NSArray *)data;
-(void)CkNotAllSelect;
-(void)CkAllSelect;
@end #import "CheckView.h"
#import "CommonButton.h"
#define Kcount 3 @interface CheckView (){
UITableView *_tableview;
} @property(nonatomic,retain)NSArray *MyData;
@property(nonatomic,retain)NSMutableArray *chooseArrary;
@end @implementation CheckView - (id)initWithFrame:(CGRect)frame Data:(NSArray *)data
{
self = [super initWithFrame:frame];
if (self) {
self.MyData=data;
_tableview=[UITableView TableViewWithFrame:self.bounds style:UITableViewStylePlain backgroundColor:[UIColor clearColor] delegate:self separatorStyle:UITableViewCellSeparatorStyleSingleLine];
_tableview.rowHeight=;
[self addSubview:_tableview];
self.chooseArrary =[NSMutableArray arrayWithCapacity:];
for (int i=; i<data.count; i++) {
[self.chooseArrary addObject:[NSNumber numberWithBool:NO]];
} }
return self;
} -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return ; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.MyData.count%Kcount==?self.MyData.count/Kcount:self.MyData.count/Kcount+;
} -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIndentify=@"CheckView";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIndentify];
if (cell==nil) {
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentify] autorelease];
}
int row=indexPath.row*Kcount;
int width=tableView.bounds.size.width/Kcount;
CGRect leftRect,rect=CGRectMake(, , tableView.bounds.size.width, tableView.rowHeight);
for(int i=row;i<row+Kcount;i++){
if(i>=self.MyData.count) break;
else{
CGRectDivide(rect, &leftRect, &rect, width, CGRectMinXEdge);
CommonButton *btn=[[CommonButton alloc] initWithFrame:leftRect];
[btn setTitle:self.MyData[i] forState:UIControlStateNormal];
BOOL flag=[[self.chooseArrary objectAtIndex:i] boolValue];
if (flag) {
[btn setImage:[UIImage imageNamed:@"radio_selected"] forState:UIControlStateNormal]; }
else{
[btn setImage:[UIImage imageNamed:@"radio_normal"] forState:UIControlStateNormal];
} [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
btn.tag=i;
[cell addSubview:btn];
}
} return cell;
} -(void)click:(CommonButton *)btn{
NSLog(@"-->%zi",btn.tag);
int tag=btn.tag;
BOOL flag=[[self.chooseArrary objectAtIndex:tag] boolValue];
if (flag) {
[self.chooseArrary replaceObjectAtIndex:tag withObject:[NSNumber numberWithBool:!flag]];
[btn setImage:[UIImage imageNamed:@"radio_normal"] forState:UIControlStateNormal]; }
else{
[self.chooseArrary replaceObjectAtIndex:tag withObject:[NSNumber numberWithBool:!flag]];
[btn setImage:[UIImage imageNamed:@"radio_selected"] forState:UIControlStateNormal];
}
} -(void)CkAllSelect{
for (int i=; i<self.MyData.count;i++) {
[self.chooseArrary replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:YES]]; }
[_tableview reloadData]; } -(void)CkNotAllSelect{
for (int i=; i<self.MyData.count;i++) {
[self.chooseArrary replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:NO]]; }
[_tableview reloadData];
} - (void)dealloc
{
[_chooseArrary release];
[_MyData release];
[super dealloc];
} @end
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// [self RadioButtonSelect];
// UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
// btn.frame=CGRectMake(0, 100, 50,50);
// [self.view addSubview:btn];
// [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
NSArray *arr=@[@"标题1",@"标题2",@"标题3",@"标题4"];
CheckView *checkView=[[CheckView alloc] initWithFrame:CGRectMake(, , , ) Data:arr];
_CK=checkView;
[self.view addSubview:checkView];
[checkView release]; }

Ios中checkBox的更多相关文章

  1. iOS中textbox文本框清除圆角

    在iOS.Mac safari中,所有的textbox, select, checkbox都会被强制美化为圆角.但在特殊情况下需要清除圆角时发现iOS中使用以下传统的css无效: border-rad ...

  2. 解决ios中input兼容性问题

    1.解决input输入框在iOS中有阴影问题 input{ -webkit-appearance: none; } 2.checkbox.raido在ios中阴影问题 单选:              ...

  3. iOS中支付宝集成

    iOS中支付宝集成 如今各种的App中都使用了三方支付的功能,现在将我在使用支付宝支付集成过程的心得分享一下,希望对大家都能有所帮助 要集成一个支付宝支付过程的环境,大致需要: 1>公司:先与支 ...

  4. iOS中数据库应用基础

    iOS 数据库入门 一.数据库简介 1.什么是数据库? 数据库(Database) 是按照数据结构来组织,存储和管理数据的仓库 数据库可以分为2大种类 关系型数据库(主流) PC端 Oracle My ...

  5. 正则表达式在iOS中的运用

    1.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...

  6. iOS 中的 HotFix 方案总结详解

    相信HotFix大家应该都很熟悉了,今天主要对于最近调研的一些方案做一些总结.iOS中的HotFix方案大致可以分为四种: WaxPatch(Alibaba) Dynamic Framework(Ap ...

  7. iOS中使用正则

    一.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...

  8. IOS中div contenteditable=true无法输入

    在IOS中<div contenteditable="true"></div>中点击时可以弹出键盘但是无法输入.加一个样式-webkit-user-sele ...

  9. 谈谈iOS中的屏幕方向

    众所周知,iOS中提供了[UIDevice currentDevice].orientation与[UIApplication sharedApplication].statusBarOrientat ...

随机推荐

  1. [leetcode]Best Time to Buy and Sell Stock @ Python

    原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/ 题意: Say you have an array for ...

  2. PHP操作数据库函数比较

    常用的php语法连接mysql如下 <?php $link = mysql_connect('localhost', 'user', 'password'); mysql_select_db(' ...

  3. DNS预解析dns-prefetch提升页面载入速度优化前端性能

    当浏览器请求一个URL的时候,通过firebug我们可以发现大概有以下几个过程:阻挡.域名解析.建立连接.发送请求.等待响应.接收数据.后面四个跟用户的网络情况和你的服务器处理速度有关,本文重点说说前 ...

  4. Cookie常用的优化方式

    我们知道,对于每一个http请求,无论是动态的网页aspx,还是jsp,还是/user/add这种MVC模式的URL,还是CSS或者JS文件,每一个http请求都会带着在硬盘上存有的跟这个域相关存有的 ...

  5. 强化学习之Q-learning简介

    https://blog.csdn.net/Young_Gy/article/details/73485518 强化学习在alphago中大放异彩,本文将简要介绍强化学习的一种q-learning.先 ...

  6. spring mvc 学习01 搭建

    /** * */ /** * @author Administrator * */ package com.study01.controller; import org.springframework ...

  7. Java-JUC(十):线程按序交替执行

    问题: 有a.b.c三个线程,使得它们按照abc依次执行10次. 实现: package com.dx.juc.test; import java.util.concurrent.locks.Cond ...

  8. glValidateProgram只用于调试

    glValidateProgram应该只用于调试,用于release版本中会影响性能.以下是详细描述:   Before doing so, however, we might want to che ...

  9. 【Nodejs】外研社一年级起三年级下MP3下载爬虫1.00

    //====================================================== // 外研社一年级起三年级下MP3下载爬虫1.00 // 2018年5月9日 //== ...

  10. Java Synchronized 关键字

    本文内容 Synchronized 关键字 示例 Synchronized 方法 内部锁(Intrinsic Locks)和 Synchronization 参考资料 下载 Demo Synchron ...