思路:
众所周知,UiTableView的功能非常的强大,有添加,删除,移动等对cell操作的功能。
在使用短信的时候,如果手机中的短信相当多的情况洗,如果一条一条的删除,那可早了活了!
所以短信中有同时选择多条短信的功能。
本人的复选框就是从中得到想法,利用TableView实现,可以实现传值,如果你有更好的传值方法,不要藏着哦!
运行效果:
代码片段:(已注释)
本人偷了个懒,直接创建了一个Single View Application ,使用的xib布局。源码地址在最下方
一、viewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
//为了多选
self.tableview.allowsSelection=YES;
self.tableview.allowsSelectionDuringEditing=YES;
//直接让tableView处于编辑状态
[self.tableview setEditing:YES animated:YES];
//设置tableView无分割线
self.tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
self.tableview.backgroundColor=[UIColor clearColor];
//tableView显示大小
self.tableview.frame=CGRectMake(50, 50, 220, 300);
//垂直方向滑块取消
self.tableview.showsVerticalScrollIndicator=NO;
//tableView总大小
self.tableview.contentSize=CGSizeMake(220, 300);
//边界无回弹
self.tableview.bounces=NO;
}
二、tableView部分设置
//tableView几部分
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//tableView 每个部分Title
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"哪些符合你?(多选)";
}
//tableView 每个部分Title高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}
//tableView 每个cell 高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
//cell数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
arr=[[NSArray alloc]initWithObjects:@"1.饮食偏油腻", @"2.经常吃零食",@"3.喝酒频繁",@"4.爱喝饮料",@"5.以上均不符合",nil];
return arr.count;
}
//cell内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellTable=@"Cell";
SelectionCell * cell=[tableView dequeueReusableCellWithIdentifier:CellTable];
if(cell==nil)
{
cell=[[SelectionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTable];
//cell.backgroundColor=[UIColor clearColor];
}
if (indexPath.row<arr.count) {
NSString *str=[arr objectAtIndex:indexPath.row];
NSLog(@"%@",str);
cell.textLabel.text = str;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:16.0f];
}
return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//不要显示任何编辑的图标
return UITableViewCellEditingStyleNone;
}
static int i=0;
/*
00000 0一个都没选
00001 0选中第一个
00011 0选中第一个,第二个
00111 0
01111 0
11111 0全选
10000 0选中第五个(只要>=16,就为健康)
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//编辑中状态的cell选择
if (!self.arr || indexPath.section >= self.arr.count) {
return;
}
//更新cell的选择状态
SelectionCell *cell = (SelectionCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.checked = !cell.checked;
[cell setChecked:cell.checked];
int k=1;
for(int j=0;j<indexPath.row;j++)
{
k*=2;
}
NSLog(@"k=%d",k);
if(cell.checked)
{
i+=k;
}
else
{
i-=k;
}
if(indexPath.row==4)
{
//NSLog(@"last");
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"您的选择" message:@"均不符合" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
}
NSLog(@"i=%d",i);
}
//按钮点击事件
-(IBAction)submit:(id)sender
{
if(i==0)
{
NSLog(@"正经点!");
}
else if(i>=16)
{
NSLog(@"健康");
}
else
{
NSLog(@"注意啦!");
}
}
三、selectionCell.h
@interface SelectionCell : UITableViewCell
{
@private
//是否选中状态的图标
UIImageView *_imgSelectionMark;
//是否选中状态的变量
BOOL _isSelected;
}
@property(nonatomic, assign) BOOL checked;
- (void)setChecked:(BOOL)checked;
@end
四、selectionCell.m
#import "SelectionCell.h"
@implementation SelectionCell
@synthesize checked = _isSelected;
- (void) setEditing:(BOOL)editting animated:(BOOL)animated
{
//不要破坏原本的系统动作
[super setEditing:editting animated:animated];
//进入编辑状态
//背景视图生成,以准备设置选中和未选中的不同背景色
self.backgroundView = [[UIView alloc] init];
self.backgroundView.backgroundColor = [UIColor whiteColor];
_imgSelectionMark = [[UIImageView alloc] initWithFrame:CGRectMake(6.0f,CGRectGetHeight(self.bounds)/2 - 14.5f,29.0f,29.0f)];
_imgSelectionMark.alpha = 1.0f;
[self addSubview:_imgSelectionMark];
//更新选中与否的界面显示
[self setChecked:_isSelected];
}
- (void) setChecked:(BOOL)checked
{
//选中
if (checked)
{
//勾选的图标
_imgSelectionMark.image = [UIImage imageNamed:@"CheckBoxYes"];
//勾选状态的背景颜色
self.textLabel.textColor = [UIColor blueColor];
self.detailTextLabel.textColor = [UIColor whiteColor];
}
//反选
else
{
//反选的图标
_imgSelectionMark.image = [UIImage imageNamed:@"CheckBoxNo"];
self.textLabel.textColor = [UIColor blackColor];
self.detailTextLabel.textColor = [UIColor grayColor];
}
//需要记录到成员量中
_isSelected = checked;
}
@end
源码地址: http://download.csdn.net/detail/xiaomi_dalianmifen/6319269