UI控件笔记(十二):UI之TableEdit\ UISearchBar\自定义实时搜索\ 索引
2016-05-17 10:26
621 查看
一、TableEdit的编辑和自定义TableEdit编辑(增加、减少、多选)
系统自带:AAAA
自定义:BBBB
#import "MainViewController.h"
@interface
MainViewController ()<UITableViewDataSource,UITableViewDelegate>
{
BOOL _isEdit;//判断是否显示多选框
}
@property(nonatomic,retain)NSMutableArray *dataArr;
@property(nonatomic,retain)NSMutableArray *selectArr;//记录我们选中的那些行的数组
———AAAA
@property(nonatomic,retain)NSMutableArray *customSelectArr;//记录自定义多选删除内容的数组
———BBBB
@property(nonatomic,retain)NSMutableArray *btnStatusArr;//记录每一行按钮的状态的数组———BBBB
@end
@implementation MainViewController
-(void)dealloc
{
self.btnStatusArr =
nil;
self.customSelectArr =
nil;
self.selectArr =
nil;
self.dataArr =
nil;
[super dealloc];
}
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if(self)
{
self.dataArr = [NSMutableArray
arrayWithCapacity:0];
self.selectArr = [NSMutableArray
arrayWithCapacity:0];
self.customSelectArr = [NSMutableArray
arrayWithCapacity:0];
self.btnStatusArr = [NSMutableArray
arrayWithCapacity:0];
_isEdit =
NO;
}
return
self;
}
- (void)viewDidLoad {
[super
viewDidLoad];
self.automaticallyAdjustsScrollViewInsets =
NO;
[self loadData];
[self makeUI];
[self
makeCustomBtn];
// Do any additional setup after loading the view.
}
-(void)makeCustomBtn
———— BBBB
{
NSArray *arr =
@[@"增加",@"编辑",@"一键删除"];
for(int i =
0;i<3;i++)
{
UIButton *btn = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
btn.frame =
CGRectMake(i*110,
64, 110, 40);
[btn setTitle:arr[i]
forState:UIControlStateNormal];
[btn addTarget:self
action:@selector(customBtnDown:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:btn];
btn.tag =
1800+i;
}
}
-(void)customBtnDown:(UIButton*)btn ———— BBBB
{
switch (btn.tag) {
case 1800:
{//增加
//向数据源里添加数据
[self.dataArr
addObject:[NSString
stringWithFormat:@"第%ld行",self.dataArr.count]];//长度20,数据0到19,所以20就是下一个
[self.btnStatusArr
addObject:@"0"];
break;
}
case 1801:
{//编辑
_isEdit = !_isEdit;
for(int i =
0;i<self.btnStatusArr.count;i++)
{
[self.btnStatusArr
replaceObjectAtIndex:i withObject:@"0"];//状态归零
}
[self.customSelectArr
removeAllObjects];//清空弃儿区
break;
}
case 1802:
{//一键删除
//把多选的那些行从数据源里移除
[self.dataArr
removeObjectsInArray:self.customSelectArr];//把customSelectArr里的数组从dataArr里删除
[self.btnStatusArr
removeObject:@"1"];
break;
}
default:
break;
}
//刷新table
UITableView *table = (UITableView*)[self.view
viewWithTag:6000];
[table reloadData];
}
-(void)loadData
{
//做点假数据
for(int i =
0;i<20;i++)
{
[self.dataArr
addObject:[NSString
stringWithFormat:@"第%d行",i]];
[self.btnStatusArr
addObject:@"0"];//0没选中,1选中了
}
}
-(void)makeUI
{
UITableView *table = [[UITableView
alloc] initWithFrame:CGRectMake(0,
104,
320, self.view.frame.size.height-104)
style:UITableViewStylePlain];
table.dataSource =
self;
table.delegate =
self;
[self.view
addSubview:table];
[table release];
table.tag = 6000;
//做一个编辑按钮
self.navigationItem.rightBarButtonItem =
self.editButtonItem;
}
//编辑按钮会触发的方法就是来制作一个每次改变的bool值
-(void)setEditing:(BOOL)editing animated:(BOOL)animated ———— AAAA
{
[super
setEditing:editing
animated:animated];//如果实现了当前的setEditing方法,需要用super
先执行setEditing来改变editing这个bool值
NSLog(@"%d",editing);
//找到table,根据editing的值让table改变为编辑或者普通模式
UITableView *table = (UITableView*)[self.view
viewWithTag:6000];
//下面就是改变table的编辑或者普通状态,第一个参数是一个bool值,就是上面super每次改变了的bool值,一次为0一次为1
[table setEditing:editing
animated:animated];
//在这里判断是YES还是NO,yes的话(开始编辑)这里什么也不干,no(结束编辑)这里把数据源修改一下,删除那些被多选的行
if(editing ==
NO)//为NO的状态,table结束编辑,修改数据源
{
[self.dataArr
removeObjectsInArray:self.selectArr];//把selectArr里的东西,从dataArr里删除
[table reloadData];
}
else//只有编辑状态的时候,selectArr里面存的东西才算数
{//table开始编辑
[self.selectArr
removeAllObjects];//把选中数组的数据清空(上一次选过的数据,和非编辑状态下选中的那一行的数据)
}
}
#pragma mark table的代理方法们
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return
self.dataArr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger section = indexPath.section;
NSInteger row = indexPath.row;
static NSString *iden =
@"iden";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:iden];
//3、判断cell是否存在
if(cell ==
nil)//判断是否取到了,nil就是没取到
{
//实例化一个cell
cell = [[[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:iden] autorelease];
}
//如果没有自定义cell,只是在用cell自己的属性的时候,不要清空cell
for(id temp
in cell.contentView.subviews)
{
[temp removeFromSuperview];
}
//cell.textLabel.text = self.dataArr[row];
UILabel *nameLabel = [[UILabel
alloc] initWithFrame:CGRectMake(10,
5, 100,
20)];
nameLabel.text =
self.dataArr[indexPath.row];
[cell.contentView
addSubview:nameLabel];
[nameLabel release];
UIButton *multiSelectBtn = [UIButton
buttonWithType:UIButtonTypeCustom];
multiSelectBtn.frame =
CGRectMake(120,
5, 23, 23);
[multiSelectBtn setImage:[UIImage
imageNamed:@"answer_much_normal.png"]
forState:UIControlStateNormal];
[multiSelectBtn addTarget:self
action:@selector(multiBtnDown:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView
addSubview:multiSelectBtn];
multiSelectBtn.tag = indexPath.row+1000;
if(_isEdit)
{
multiSelectBtn.hidden =
NO;
}
else
{
multiSelectBtn.hidden =
YES;
}
UIButton *singleDeleteBtn = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
singleDeleteBtn.frame =
CGRectMake(250,
5, 60, 23);
[singleDeleteBtn setTitle:@"del"
forState:UIControlStateNormal];
[singleDeleteBtn addTarget:self
action:@selector(btnDown:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView
addSubview:singleDeleteBtn];
singleDeleteBtn.tag = indexPath.row +
1900;
return cell;
}
//多选按钮事件
-(void)multiBtnDown:(UIButton*)btn
———— BBBB
{
//btnStatusArr里存的是0、1
if([self.btnStatusArr[btn.tag-
1000] isEqualToString:@"0"])//判断是0还是1
{//0进来没选中,要变成选中,要添加到customSelectArr中,改成1
[btn setImage:[UIImage
imageNamed:@"answer_much_select.png"]
forState:UIControlStateNormal];//改变按钮的图片为选中图片
[self.customSelectArr
addObject:self.dataArr[btn.tag-1000]];//把这一行的数据存进准备多选删除的数组
[self.btnStatusArr
replaceObjectAtIndex:btn.tag-1000
withObject:@"1"];//把状态数组该行所在位改为1选中状态
}
else
{//1进来,要变成没选中,要从customSelectArr中移除,改成0
[btn setImage:[UIImage
imageNamed:@"answer_much_normal.png"]
forState:UIControlStateNormal];
[self.customSelectArr
removeObject:self.dataArr[btn.tag-1000]];
[self.btnStatusArr
replaceObjectAtIndex:btn.tag-1000
withObject:@"0"];
}
//1、改变当前按钮的图片
//2、改变status数组中的0、1
//3、如果这行这次是选中添加到customSelectArr中,如果这行这次是非选中,从customSelectArr中移除
}
//单选删除按钮事件
-(void)btnDown:(UIButton*)btn ———— BBBB
{
//找到行
int row = btn.tag -
1900;
//从数据源里把这行对应的数据删了
[self.dataArr
removeObjectAtIndex:row];
//刷新table
UITableView *table = (UITableView*)[self.view
viewWithTag:6000];
[table reloadData];
}
//设置table的编辑类型
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
———— AAAA
{
return
UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
//一会儿将从数据源中,把selectArr里的数据清除掉
//选中
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
———— AAAA
{
//向selectArr添数据
//找到被选中这行的数据
NSString *str =
self.dataArr[indexPath.row];
//把数据存入selectArr
[self.selectArr
addObject:str];
NSLog(@"%@",self.selectArr);
}
//反选
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
———— AAAA
{
//从selectArr删数据
//找到被反选这行的数据
NSString *str =
self.dataArr[indexPath.row];
//从selectArr中把str删除
if([self.selectArr
containsObject:str])//判断数组中是否有一个数据对象,返回值是yes或者no
{
[self.selectArr
removeObject:str];
}
NSLog(@"%@",self.selectArr);
}
二、UISearchBar和UISearchDisplayController(实时搜索)
#import "MainViewController.h"
@interface
MainViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
{
UISearchDisplayController *_display;//搜索结果的display成员变量,这里做成全局变量主要是考虑内存释放时机
}
@property(nonatomic,retain)NSMutableArray *dataArr;
@property(nonatomic,retain)NSMutableArray *resultArr;//用来保存搜索结果数据的数组
@end
@implementation MainViewController
-(void)dealloc
{
[_display release];
self.resultArr =
nil;
self.dataArr =
nil;
[super dealloc];
}
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if(self)
{
self.dataArr = [NSMutableArray
arrayWithCapacity:0];
self.resultArr = [NSMutableArray
arrayWithCapacity:0];
}
return
self;
}
- (void)viewDidLoad {
[super
viewDidLoad];
self.automaticallyAdjustsScrollViewInsets =
NO;
[self loadData];
[self makeUI];
}
-(void)loadData
{
NSArray *arr =
@[@"白宇",@"徐鹏",@"孙冠宇",@"王勇",@"刘佳坤",@"芦艺",@"陈雷",@"朱丽丽",@"乔柏智",@"武鑫",@"李鹏飞",@"李煜",@"马强",@"韦庆江",@"那子玉",@"司文",@"吴鑫",@"邬林",@"韩淑惠",@"黄世伟",@"彭佳伟",@"王兴隆",@"吴雪松",@"王硕",@"杨林杰",@"李多军",@"赵明亮",@"邵运普",@"吴伟",@"郭峰泉",@"杨桁",@"于海尧",@"董强飞",@"王洋",@"高兵",@"范森军",@"杨胜超",@"赵丹",@"刘婉玲",@"王林",@"彭文杰",@"殷宪鹏",@"陈万柱",@"张显忠",@"蔡宇航",@"吕夏",@"马凡",@"王蕾",@"冯杨",@"王志超",@"刘文正",@"郑连杰",@"刘海洋",@"刘丹",@"张磊",@"宋昆达",@"陈伯怀",@"潘俊峰",@"何嘉伟",@"王行远",@"韩德顺",@"高明辉",@"赵华峰",@"赵俊隆",@"李传良",@"钱兆",@"聂康",@"罗红彪",@"刘伟",@"赵洪斌",@"王建军",@"寇明豪",@"姚成",@"张振兴",@"姜娜",@"郭德海",@"陈宇峰",@"韩卫星",@"曹猛",@"孙洪瑞",@"李孟东"];
for(int i =
0;i<arr.count;i++)
{
[self.dataArr
addObject:arr[i]];
}
}
-(void)makeUI
{
//搜索框
UISearchBar *searchBar = [[UISearchBar
alloc] initWithFrame:CGRectMake(10,
0, 300,
30)];//此处的searchbar也应该做成全局变量
//searchBar.delegate = self;
searchBar.tag =
13000;
//table
UITableView *table = [[UITableView
alloc] initWithFrame:CGRectMake(0,
64,
320, self.view.frame.size.height-64)
style:UITableViewStylePlain];
table.dataSource =
self;
table.delegate =
self;
[self.view
addSubview:table];
[table release];
table.tag = 6666;
table.tableHeaderView = searchBar;//一般都把搜索框放到一个table的头上
//display(展示搜索出来的东西用的table,也是个table)
_display = [[UISearchDisplayController
alloc] initWithSearchBar:searchBar
contentsController:self];//第一个参数是哪儿搜我有反应,第二个参数是我显示在哪个VC
_display.searchResultsDataSource =
self;
_display.searchResultsDelegate =
self;
}
#pragma markTable代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//因为display也是一个table那样的控件,所以他也会走table的代理方法
//所以要在table的代理方法中判断,是我们的table还是display的table
if(tableView.tag ==
6666)
{
return self.dataArr.count;
}
else
{//display
//根据查找结果制作一个查找结果的数组,然后把查找结果数组的长度返回
//1、清空上次查找的结果
[self.resultArr
removeAllObjects];
//2、开始这次的查找
for(NSString *str
in self.dataArr)//遍历数据源
{//把数据源里符合我们要求的内容存进结果数组
UISearchBar *searchBar = (UISearchBar*)[self.view
viewWithTag:13000];
NSRange range = [str
rangeOfString:searchBar.text];//在数组源每一位的字符串中,查找是否有我们当前输入的内容
if(range.location !=
NSNotFound)
{//找到了
[self.resultArr
addObject:str];//把数据源该位的内容存进结果数组
}
}
//3、把这次查找的结果存进结果数组
//4、把结果数组的长度返回
return
self.resultArr.count;//返回搜索结果的table的长度
}
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *iden =
@"pp";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:iden];
if(cell == nil)
{
cell = [[[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:iden] autorelease];
}
if(tableView.tag ==
6666)
{
cell.textLabel.text =
self.dataArr[indexPath.row];
}
else
{//display
cell.textLabel.text =
self.resultArr[indexPath.row];
}
return cell;
}
三、自定义实时搜索
#import "MainViewController.h"
@interface
MainViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{
BOOL _isEdit;//判断是否在输入,也就是判断是否正在查找,用来设置table的数据源是dataArr还是resultArr;
}
@property(nonatomic,retain)NSMutableArray *dataArr;
@property(nonatomic,retain)NSMutableArray *resultArr;
@end
@implementation MainViewController
-(void)dealloc
{
self.resultArr =
nil;
self.dataArr =
nil;
[super dealloc];
}
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if(self)
{
self.dataArr = [NSMutableArray
arrayWithCapacity:0];
self.resultArr = [NSMutableArray
arrayWithCapacity:0];
_isEdit =
NO;//初始化为NO,没有开始搜索,用dataArr当数据源
}
return
self;
}
- (void)viewDidLoad {
[super
viewDidLoad];
self.automaticallyAdjustsScrollViewInsets =
NO;
[self loadData];
[self makeUI];
}
-(void)loadData
{
for(int i =
0;i<100;i++)
{
[self.dataArr
addObject:[NSString
stringWithFormat:@"%d",i]];
}
}
-(void)makeUI
{
//1、table
UITableView *table = [[UITableView
alloc] initWithFrame:CGRectMake(0,
104,
320, self.view.frame.size.height-104)
style:UITableViewStylePlain];
table.dataSource =
self;
table.delegate =
self;
[self.view
addSubview:table];
[table release];
table.tag = 6666;
//2、搜索框
UITextField *text = [[UITextField
alloc] initWithFrame:CGRectMake(10,
69, 300,
30)];
text.borderStyle =
UITextBorderStyleRoundedRect;
text.delegate =
self;
[self.view
addSubview:text];
[text release];
text.tag = 5555;
[text addTarget:self
action:@selector(textEditChange:)
forControlEvents:UIControlEventEditingChanged];//当输入发生改变时调用对应方法
}
#pragma mark textField的代理
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
_isEdit =
YES;//开始编辑,所以准备要用resultArr当数据源
UIView *ff = (UIView*)[self.view
viewWithTag:4000];
//所有UIView的子类都可以用这个动画,UIView动画
[UIView
animateWithDuration:2
animations:^{
//想写啥写啥
textField.frame =
CGRectMake(10,
69, 240, 30);
}];
//textField.frame = CGRectMake(10, 69, 240, 30);
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
_isEdit =
NO;//结束编辑,用dataArr当数据源
//刷新table,让table改为用dataArr当数据源
UITableView *table = (UITableView*)[self.view
viewWithTag:6666];
[table reloadData];
textField.text =
@"";//清空搜索框
[self.resultArr
removeAllObjects];//清空搜索结果数组
[UIView
animateWithDuration:2
animations:^{
textField.frame =
CGRectMake(10,
69, 300, 30);
}];
[textField resignFirstResponder];
return
YES;
}
//随时关注textField的内容改变
-(void)textEditChange:(UITextField*)text
{//从数据源里查找有没有我们输入的textfield的内容,这个方法因为每多写一个字就调用一次,所以就是实时查找了
//1、清空上次查找结果
[self.resultArr
removeAllObjects];
//2、查找
for(NSString *str
in self.dataArr)//遍历数据源查找
{
//text.text就是输入框里的文字
if([str rangeOfString:text.text].location !=
NSNotFound)
{//找到了进
[self.resultArr
addObject:str];//把数据源中对应的数据存进结果数组
}
}
//3、刷新table
UITableView *table = (UITableView*)[self.view
viewWithTag:6666];
[table reloadData];
}
#pragma mark table的代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(_isEdit ==
NO)//判断是否在查找中
return self.dataArr.count;//原始table
else
return self.resultArr.count;//搜索table
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *iden =
@"pp";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:iden];
if(cell == nil)
{
cell = [[[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:iden] autorelease];
}
if(_isEdit ==
NO)
cell.textLabel.text =
self.dataArr[indexPath.row];
else
cell.textLabel.text =
self.resultArr[indexPath.row];
return cell;
}
四、索引(就是屏幕右边一竖行的字母)
#import "MainViewController.h"
@interface
MainViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSMutableArray *dataArr;
@end
@implementation MainViewController
-(void)dealloc
{
self.dataArr =
nil;
[super dealloc];
}
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if(self)
{
self.dataArr = [NSMutableArray
arrayWithCapacity:0];
}
return
self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets =
NO;
[self loadData];
[self makeUI];
}
-(void)loadData
{
//有段有行的data数据源,索引引导的就是段
for(int i =
'A';i<= 'Z';i++)
{
NSMutableArray *arr = [NSMutableArray
arrayWithCapacity:0];//这个数组给小循环用
for(int j =
0;j<9;j++)
{
[arr addObject:[NSString
stringWithFormat:@"%c%d",i,j]];
}
[self.dataArr
addObject:arr];
}
NSLog(@"%@",self.dataArr);
}
-(void)makeUI
{
UITableView *table = [[UITableView
alloc] initWithFrame:CGRectMake(0,
64,
320, self.view.frame.size.height-64)
style:UITableViewStylePlain];
table.dataSource =
self;
table.delegate =
self;
[self.view
addSubview:table];
[table release];
}
#pragma mark table代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{//行
return [self.dataArr[section]
count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//瓤
static NSString *iden =
@"pp";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:iden];
if(cell == nil)
{
cell = [[[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:iden] autorelease];
}
cell.textLabel.text =
self.dataArr[indexPath.section][indexPath.row];
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{//段
return
self.dataArr.count;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString
stringWithFormat:@"%C",section+'A'];
}
//设置table的索引,索引内容应该和段头一样
-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSMutableArray *arr = [NSMutableArray
arrayWithCapacity:0];
for(int i =
'A';i<='Z';i++)
{
[arr addObject:[NSString
stringWithFormat:@"%c",i]];
}
return arr;
}
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
NSLog(@"%@",title);
NSLog(@"%ld",(long)index);
return index;
}
系统自带:AAAA
自定义:BBBB
#import "MainViewController.h"
@interface
MainViewController ()<UITableViewDataSource,UITableViewDelegate>
{
BOOL _isEdit;//判断是否显示多选框
}
@property(nonatomic,retain)NSMutableArray *dataArr;
@property(nonatomic,retain)NSMutableArray *selectArr;//记录我们选中的那些行的数组
———AAAA
@property(nonatomic,retain)NSMutableArray *customSelectArr;//记录自定义多选删除内容的数组
———BBBB
@property(nonatomic,retain)NSMutableArray *btnStatusArr;//记录每一行按钮的状态的数组———BBBB
@end
@implementation MainViewController
-(void)dealloc
{
self.btnStatusArr =
nil;
self.customSelectArr =
nil;
self.selectArr =
nil;
self.dataArr =
nil;
[super dealloc];
}
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if(self)
{
self.dataArr = [NSMutableArray
arrayWithCapacity:0];
self.selectArr = [NSMutableArray
arrayWithCapacity:0];
self.customSelectArr = [NSMutableArray
arrayWithCapacity:0];
self.btnStatusArr = [NSMutableArray
arrayWithCapacity:0];
_isEdit =
NO;
}
return
self;
}
- (void)viewDidLoad {
[super
viewDidLoad];
self.automaticallyAdjustsScrollViewInsets =
NO;
[self loadData];
[self makeUI];
[self
makeCustomBtn];
// Do any additional setup after loading the view.
}
-(void)makeCustomBtn
———— BBBB
{
NSArray *arr =
@[@"增加",@"编辑",@"一键删除"];
for(int i =
0;i<3;i++)
{
UIButton *btn = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
btn.frame =
CGRectMake(i*110,
64, 110, 40);
[btn setTitle:arr[i]
forState:UIControlStateNormal];
[btn addTarget:self
action:@selector(customBtnDown:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:btn];
btn.tag =
1800+i;
}
}
-(void)customBtnDown:(UIButton*)btn ———— BBBB
{
switch (btn.tag) {
case 1800:
{//增加
//向数据源里添加数据
[self.dataArr
addObject:[NSString
stringWithFormat:@"第%ld行",self.dataArr.count]];//长度20,数据0到19,所以20就是下一个
[self.btnStatusArr
addObject:@"0"];
break;
}
case 1801:
{//编辑
_isEdit = !_isEdit;
for(int i =
0;i<self.btnStatusArr.count;i++)
{
[self.btnStatusArr
replaceObjectAtIndex:i withObject:@"0"];//状态归零
}
[self.customSelectArr
removeAllObjects];//清空弃儿区
break;
}
case 1802:
{//一键删除
//把多选的那些行从数据源里移除
[self.dataArr
removeObjectsInArray:self.customSelectArr];//把customSelectArr里的数组从dataArr里删除
[self.btnStatusArr
removeObject:@"1"];
break;
}
default:
break;
}
//刷新table
UITableView *table = (UITableView*)[self.view
viewWithTag:6000];
[table reloadData];
}
-(void)loadData
{
//做点假数据
for(int i =
0;i<20;i++)
{
[self.dataArr
addObject:[NSString
stringWithFormat:@"第%d行",i]];
[self.btnStatusArr
addObject:@"0"];//0没选中,1选中了
}
}
-(void)makeUI
{
UITableView *table = [[UITableView
alloc] initWithFrame:CGRectMake(0,
104,
320, self.view.frame.size.height-104)
style:UITableViewStylePlain];
table.dataSource =
self;
table.delegate =
self;
[self.view
addSubview:table];
[table release];
table.tag = 6000;
//做一个编辑按钮
self.navigationItem.rightBarButtonItem =
self.editButtonItem;
}
//编辑按钮会触发的方法就是来制作一个每次改变的bool值
-(void)setEditing:(BOOL)editing animated:(BOOL)animated ———— AAAA
{
[super
setEditing:editing
animated:animated];//如果实现了当前的setEditing方法,需要用super
先执行setEditing来改变editing这个bool值
NSLog(@"%d",editing);
//找到table,根据editing的值让table改变为编辑或者普通模式
UITableView *table = (UITableView*)[self.view
viewWithTag:6000];
//下面就是改变table的编辑或者普通状态,第一个参数是一个bool值,就是上面super每次改变了的bool值,一次为0一次为1
[table setEditing:editing
animated:animated];
//在这里判断是YES还是NO,yes的话(开始编辑)这里什么也不干,no(结束编辑)这里把数据源修改一下,删除那些被多选的行
if(editing ==
NO)//为NO的状态,table结束编辑,修改数据源
{
[self.dataArr
removeObjectsInArray:self.selectArr];//把selectArr里的东西,从dataArr里删除
[table reloadData];
}
else//只有编辑状态的时候,selectArr里面存的东西才算数
{//table开始编辑
[self.selectArr
removeAllObjects];//把选中数组的数据清空(上一次选过的数据,和非编辑状态下选中的那一行的数据)
}
}
#pragma mark table的代理方法们
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return
self.dataArr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger section = indexPath.section;
NSInteger row = indexPath.row;
static NSString *iden =
@"iden";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:iden];
//3、判断cell是否存在
if(cell ==
nil)//判断是否取到了,nil就是没取到
{
//实例化一个cell
cell = [[[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:iden] autorelease];
}
//如果没有自定义cell,只是在用cell自己的属性的时候,不要清空cell
for(id temp
in cell.contentView.subviews)
{
[temp removeFromSuperview];
}
//cell.textLabel.text = self.dataArr[row];
UILabel *nameLabel = [[UILabel
alloc] initWithFrame:CGRectMake(10,
5, 100,
20)];
nameLabel.text =
self.dataArr[indexPath.row];
[cell.contentView
addSubview:nameLabel];
[nameLabel release];
UIButton *multiSelectBtn = [UIButton
buttonWithType:UIButtonTypeCustom];
multiSelectBtn.frame =
CGRectMake(120,
5, 23, 23);
[multiSelectBtn setImage:[UIImage
imageNamed:@"answer_much_normal.png"]
forState:UIControlStateNormal];
[multiSelectBtn addTarget:self
action:@selector(multiBtnDown:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView
addSubview:multiSelectBtn];
multiSelectBtn.tag = indexPath.row+1000;
if(_isEdit)
{
multiSelectBtn.hidden =
NO;
}
else
{
multiSelectBtn.hidden =
YES;
}
UIButton *singleDeleteBtn = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
singleDeleteBtn.frame =
CGRectMake(250,
5, 60, 23);
[singleDeleteBtn setTitle:@"del"
forState:UIControlStateNormal];
[singleDeleteBtn addTarget:self
action:@selector(btnDown:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView
addSubview:singleDeleteBtn];
singleDeleteBtn.tag = indexPath.row +
1900;
return cell;
}
//多选按钮事件
-(void)multiBtnDown:(UIButton*)btn
———— BBBB
{
//btnStatusArr里存的是0、1
if([self.btnStatusArr[btn.tag-
1000] isEqualToString:@"0"])//判断是0还是1
{//0进来没选中,要变成选中,要添加到customSelectArr中,改成1
[btn setImage:[UIImage
imageNamed:@"answer_much_select.png"]
forState:UIControlStateNormal];//改变按钮的图片为选中图片
[self.customSelectArr
addObject:self.dataArr[btn.tag-1000]];//把这一行的数据存进准备多选删除的数组
[self.btnStatusArr
replaceObjectAtIndex:btn.tag-1000
withObject:@"1"];//把状态数组该行所在位改为1选中状态
}
else
{//1进来,要变成没选中,要从customSelectArr中移除,改成0
[btn setImage:[UIImage
imageNamed:@"answer_much_normal.png"]
forState:UIControlStateNormal];
[self.customSelectArr
removeObject:self.dataArr[btn.tag-1000]];
[self.btnStatusArr
replaceObjectAtIndex:btn.tag-1000
withObject:@"0"];
}
//1、改变当前按钮的图片
//2、改变status数组中的0、1
//3、如果这行这次是选中添加到customSelectArr中,如果这行这次是非选中,从customSelectArr中移除
}
//单选删除按钮事件
-(void)btnDown:(UIButton*)btn ———— BBBB
{
//找到行
int row = btn.tag -
1900;
//从数据源里把这行对应的数据删了
[self.dataArr
removeObjectAtIndex:row];
//刷新table
UITableView *table = (UITableView*)[self.view
viewWithTag:6000];
[table reloadData];
}
//设置table的编辑类型
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
———— AAAA
{
return
UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
//一会儿将从数据源中,把selectArr里的数据清除掉
//选中
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
———— AAAA
{
//向selectArr添数据
//找到被选中这行的数据
NSString *str =
self.dataArr[indexPath.row];
//把数据存入selectArr
[self.selectArr
addObject:str];
NSLog(@"%@",self.selectArr);
}
//反选
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
———— AAAA
{
//从selectArr删数据
//找到被反选这行的数据
NSString *str =
self.dataArr[indexPath.row];
//从selectArr中把str删除
if([self.selectArr
containsObject:str])//判断数组中是否有一个数据对象,返回值是yes或者no
{
[self.selectArr
removeObject:str];
}
NSLog(@"%@",self.selectArr);
}
二、UISearchBar和UISearchDisplayController(实时搜索)
#import "MainViewController.h"
@interface
MainViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
{
UISearchDisplayController *_display;//搜索结果的display成员变量,这里做成全局变量主要是考虑内存释放时机
}
@property(nonatomic,retain)NSMutableArray *dataArr;
@property(nonatomic,retain)NSMutableArray *resultArr;//用来保存搜索结果数据的数组
@end
@implementation MainViewController
-(void)dealloc
{
[_display release];
self.resultArr =
nil;
self.dataArr =
nil;
[super dealloc];
}
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if(self)
{
self.dataArr = [NSMutableArray
arrayWithCapacity:0];
self.resultArr = [NSMutableArray
arrayWithCapacity:0];
}
return
self;
}
- (void)viewDidLoad {
[super
viewDidLoad];
self.automaticallyAdjustsScrollViewInsets =
NO;
[self loadData];
[self makeUI];
}
-(void)loadData
{
NSArray *arr =
@[@"白宇",@"徐鹏",@"孙冠宇",@"王勇",@"刘佳坤",@"芦艺",@"陈雷",@"朱丽丽",@"乔柏智",@"武鑫",@"李鹏飞",@"李煜",@"马强",@"韦庆江",@"那子玉",@"司文",@"吴鑫",@"邬林",@"韩淑惠",@"黄世伟",@"彭佳伟",@"王兴隆",@"吴雪松",@"王硕",@"杨林杰",@"李多军",@"赵明亮",@"邵运普",@"吴伟",@"郭峰泉",@"杨桁",@"于海尧",@"董强飞",@"王洋",@"高兵",@"范森军",@"杨胜超",@"赵丹",@"刘婉玲",@"王林",@"彭文杰",@"殷宪鹏",@"陈万柱",@"张显忠",@"蔡宇航",@"吕夏",@"马凡",@"王蕾",@"冯杨",@"王志超",@"刘文正",@"郑连杰",@"刘海洋",@"刘丹",@"张磊",@"宋昆达",@"陈伯怀",@"潘俊峰",@"何嘉伟",@"王行远",@"韩德顺",@"高明辉",@"赵华峰",@"赵俊隆",@"李传良",@"钱兆",@"聂康",@"罗红彪",@"刘伟",@"赵洪斌",@"王建军",@"寇明豪",@"姚成",@"张振兴",@"姜娜",@"郭德海",@"陈宇峰",@"韩卫星",@"曹猛",@"孙洪瑞",@"李孟东"];
for(int i =
0;i<arr.count;i++)
{
[self.dataArr
addObject:arr[i]];
}
}
-(void)makeUI
{
//搜索框
UISearchBar *searchBar = [[UISearchBar
alloc] initWithFrame:CGRectMake(10,
0, 300,
30)];//此处的searchbar也应该做成全局变量
//searchBar.delegate = self;
searchBar.tag =
13000;
//table
UITableView *table = [[UITableView
alloc] initWithFrame:CGRectMake(0,
64,
320, self.view.frame.size.height-64)
style:UITableViewStylePlain];
table.dataSource =
self;
table.delegate =
self;
[self.view
addSubview:table];
[table release];
table.tag = 6666;
table.tableHeaderView = searchBar;//一般都把搜索框放到一个table的头上
//display(展示搜索出来的东西用的table,也是个table)
_display = [[UISearchDisplayController
alloc] initWithSearchBar:searchBar
contentsController:self];//第一个参数是哪儿搜我有反应,第二个参数是我显示在哪个VC
_display.searchResultsDataSource =
self;
_display.searchResultsDelegate =
self;
}
#pragma markTable代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//因为display也是一个table那样的控件,所以他也会走table的代理方法
//所以要在table的代理方法中判断,是我们的table还是display的table
if(tableView.tag ==
6666)
{
return self.dataArr.count;
}
else
{//display
//根据查找结果制作一个查找结果的数组,然后把查找结果数组的长度返回
//1、清空上次查找的结果
[self.resultArr
removeAllObjects];
//2、开始这次的查找
for(NSString *str
in self.dataArr)//遍历数据源
{//把数据源里符合我们要求的内容存进结果数组
UISearchBar *searchBar = (UISearchBar*)[self.view
viewWithTag:13000];
NSRange range = [str
rangeOfString:searchBar.text];//在数组源每一位的字符串中,查找是否有我们当前输入的内容
if(range.location !=
NSNotFound)
{//找到了
[self.resultArr
addObject:str];//把数据源该位的内容存进结果数组
}
}
//3、把这次查找的结果存进结果数组
//4、把结果数组的长度返回
return
self.resultArr.count;//返回搜索结果的table的长度
}
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *iden =
@"pp";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:iden];
if(cell == nil)
{
cell = [[[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:iden] autorelease];
}
if(tableView.tag ==
6666)
{
cell.textLabel.text =
self.dataArr[indexPath.row];
}
else
{//display
cell.textLabel.text =
self.resultArr[indexPath.row];
}
return cell;
}
三、自定义实时搜索
#import "MainViewController.h"
@interface
MainViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{
BOOL _isEdit;//判断是否在输入,也就是判断是否正在查找,用来设置table的数据源是dataArr还是resultArr;
}
@property(nonatomic,retain)NSMutableArray *dataArr;
@property(nonatomic,retain)NSMutableArray *resultArr;
@end
@implementation MainViewController
-(void)dealloc
{
self.resultArr =
nil;
self.dataArr =
nil;
[super dealloc];
}
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if(self)
{
self.dataArr = [NSMutableArray
arrayWithCapacity:0];
self.resultArr = [NSMutableArray
arrayWithCapacity:0];
_isEdit =
NO;//初始化为NO,没有开始搜索,用dataArr当数据源
}
return
self;
}
- (void)viewDidLoad {
[super
viewDidLoad];
self.automaticallyAdjustsScrollViewInsets =
NO;
[self loadData];
[self makeUI];
}
-(void)loadData
{
for(int i =
0;i<100;i++)
{
[self.dataArr
addObject:[NSString
stringWithFormat:@"%d",i]];
}
}
-(void)makeUI
{
//1、table
UITableView *table = [[UITableView
alloc] initWithFrame:CGRectMake(0,
104,
320, self.view.frame.size.height-104)
style:UITableViewStylePlain];
table.dataSource =
self;
table.delegate =
self;
[self.view
addSubview:table];
[table release];
table.tag = 6666;
//2、搜索框
UITextField *text = [[UITextField
alloc] initWithFrame:CGRectMake(10,
69, 300,
30)];
text.borderStyle =
UITextBorderStyleRoundedRect;
text.delegate =
self;
[self.view
addSubview:text];
[text release];
text.tag = 5555;
[text addTarget:self
action:@selector(textEditChange:)
forControlEvents:UIControlEventEditingChanged];//当输入发生改变时调用对应方法
}
#pragma mark textField的代理
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
_isEdit =
YES;//开始编辑,所以准备要用resultArr当数据源
UIView *ff = (UIView*)[self.view
viewWithTag:4000];
//所有UIView的子类都可以用这个动画,UIView动画
[UIView
animateWithDuration:2
animations:^{
//想写啥写啥
textField.frame =
CGRectMake(10,
69, 240, 30);
}];
//textField.frame = CGRectMake(10, 69, 240, 30);
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
_isEdit =
NO;//结束编辑,用dataArr当数据源
//刷新table,让table改为用dataArr当数据源
UITableView *table = (UITableView*)[self.view
viewWithTag:6666];
[table reloadData];
textField.text =
@"";//清空搜索框
[self.resultArr
removeAllObjects];//清空搜索结果数组
[UIView
animateWithDuration:2
animations:^{
textField.frame =
CGRectMake(10,
69, 300, 30);
}];
[textField resignFirstResponder];
return
YES;
}
//随时关注textField的内容改变
-(void)textEditChange:(UITextField*)text
{//从数据源里查找有没有我们输入的textfield的内容,这个方法因为每多写一个字就调用一次,所以就是实时查找了
//1、清空上次查找结果
[self.resultArr
removeAllObjects];
//2、查找
for(NSString *str
in self.dataArr)//遍历数据源查找
{
//text.text就是输入框里的文字
if([str rangeOfString:text.text].location !=
NSNotFound)
{//找到了进
[self.resultArr
addObject:str];//把数据源中对应的数据存进结果数组
}
}
//3、刷新table
UITableView *table = (UITableView*)[self.view
viewWithTag:6666];
[table reloadData];
}
#pragma mark table的代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(_isEdit ==
NO)//判断是否在查找中
return self.dataArr.count;//原始table
else
return self.resultArr.count;//搜索table
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *iden =
@"pp";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:iden];
if(cell == nil)
{
cell = [[[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:iden] autorelease];
}
if(_isEdit ==
NO)
cell.textLabel.text =
self.dataArr[indexPath.row];
else
cell.textLabel.text =
self.resultArr[indexPath.row];
return cell;
}
四、索引(就是屏幕右边一竖行的字母)
#import "MainViewController.h"
@interface
MainViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSMutableArray *dataArr;
@end
@implementation MainViewController
-(void)dealloc
{
self.dataArr =
nil;
[super dealloc];
}
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if(self)
{
self.dataArr = [NSMutableArray
arrayWithCapacity:0];
}
return
self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets =
NO;
[self loadData];
[self makeUI];
}
-(void)loadData
{
//有段有行的data数据源,索引引导的就是段
for(int i =
'A';i<= 'Z';i++)
{
NSMutableArray *arr = [NSMutableArray
arrayWithCapacity:0];//这个数组给小循环用
for(int j =
0;j<9;j++)
{
[arr addObject:[NSString
stringWithFormat:@"%c%d",i,j]];
}
[self.dataArr
addObject:arr];
}
NSLog(@"%@",self.dataArr);
}
-(void)makeUI
{
UITableView *table = [[UITableView
alloc] initWithFrame:CGRectMake(0,
64,
320, self.view.frame.size.height-64)
style:UITableViewStylePlain];
table.dataSource =
self;
table.delegate =
self;
[self.view
addSubview:table];
[table release];
}
#pragma mark table代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{//行
return [self.dataArr[section]
count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//瓤
static NSString *iden =
@"pp";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:iden];
if(cell == nil)
{
cell = [[[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:iden] autorelease];
}
cell.textLabel.text =
self.dataArr[indexPath.section][indexPath.row];
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{//段
return
self.dataArr.count;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString
stringWithFormat:@"%C",section+'A'];
}
//设置table的索引,索引内容应该和段头一样
-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSMutableArray *arr = [NSMutableArray
arrayWithCapacity:0];
for(int i =
'A';i<='Z';i++)
{
[arr addObject:[NSString
stringWithFormat:@"%c",i]];
}
return arr;
}
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
NSLog(@"%@",title);
NSLog(@"%ld",(long)index);
return index;
}
相关文章推荐
- GCD介绍(一):基本概念和Dispatch Queue
- 导入其他工程时Build Setting设置
- UI控件笔记(十一):UI之UITableView\自定义UITableView\分组
- Codeforces Round #353 (Div. 2)-A. Infinite Sequence(模拟)
- tess-two编译失败-----NDK build for target x86_64 results in error
- UI控件笔记(十):UI之UIScrollView\两种手势\第三方库\pagecontrol\相册制作
- UI控件笔记(九):UI之UITabBarController
- UI控件笔记(八):UI之UISlider\UIStepper\ UIProgressView\ UITextView\UISwitch\ UIActivityIndicatorView\ UISeg
- 各种开源UI
- UI控件笔记(七):UI之五种传值方式 代理\单例\缓存\通知\正向
- UI控件笔记(六):UI之UINavigationController、navigationBar和UIView的封装
- UI控件笔记(五):UI之UIViewController和NSUserDefaults
- 详解 C# break ,continue, return
- UI控件笔记(四):UI之UITextField的属性
- KAlertController可更改外观的提示
- UI控件笔记(三):UI之UIButton的属性
- UIPopoverController
- easyui datagrid动态修改editor为combobox的options
- UI控件笔记(二):UI之UILabel和UIImageView
- LeetCode 187. Repeated DNA Sequences