-(void)dealSearch
{
[_searchArray removeAllObjects];
for (NSMutableArray *subArray in _dataArray) {
for (DeleteModel *model in subArray) {
if([model.data rangeOfString:_searchBar.text].location != NSNotFound)
{
[_searchArray addObject:model];
}
}
}
}
- (void)configSearch
{
CGSize size = [UIScreen mainScreen].bounds.size;
_searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, size.width, 44)];
_searchBar.placeholder = @"请搜索";
_table.tableHeaderView = _searchBar;//加到头部.
_searchArray = [[NSMutableArray alloc]init];
_sdc = [[UISearchDisplayController alloc]initWithSearchBar:_searchBar contentsController:self];
_sdc.searchResultsDataSource = self;
}
- (void)creatButton
{
UIButton *button =[UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(0, 0, 40, 20);
[button setTitle:@"编辑" forState: UIControlStateNormal];
[button addTarget:self action:@selector(editing:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:button];
self.navigationItem.leftBarButtonItem = leftItem;
}
- (void)editing:(UIButton *)buton
{
[buton setTitle:@"完成" forState:UIControlStateNormal];
_table.editing = YES;
[buton addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)edit:(UIButton *)bu
{
[bu setTitle:@"编辑" forState:UIControlStateNormal];
_table.editing = NO;
[bu addTarget:self action:@selector(editing:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - 添加数据
-(void)loadData
{
_dataArray = [[NSMutableArray alloc] init];
//生成数据
//如果是分组显示数据, 一般数据源数组中每个又是数组, 每个数据包含了数据
for (int i='A'; i<='Z'; i++) {
NSMutableArray *subArray = [[NSMutableArray alloc] init];
for (int j=0; j<10; j++) {
DeleteModel *model = [[DeleteModel alloc]init];
model.data = [NSString stringWithFormat:@"第%c组 第%d行数据",i,j];
model.isDelete = NO;
[subArray addObject:model];
}
[_dataArray addObject:subArray];
}
_arrayM = [[NSMutableArray alloc]init];
for(int i = 'A';i <= 'Z';i++)
{
[_arrayM addObject:[NSString stringWithFormat:@"%c",i]];
}
}
- (void)creatTable
{
_table = [[UITableView alloc]initWithFrame:self.view.bounds style: UITableViewStylePlain];
_table.delegate = self;
_table.dataSource = self;
[self.view addSubview:_table];
_table.separatorColor = [UIColor redColor];
_table.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
_table.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
[_table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 取消点击后的cell灰色的效果
[tableView deselectRowAtIndexPath:indexPath animated:NO];;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *array;
if(tableView != _table)
{
return _searchArray.count;
}
else
{
array = _dataArray[section];
}
return array.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(tableView != _table)
{
[self dealSearch];
return 1;
}
return _dataArray.count;
}
#pragma mark - 设置组头
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if(tableView != _table)
{
return @"搜索结果";
}
return _arrayM[section];
}
#pragma mark - 添加/删除单元格
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleInsert|UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle == UITableViewCellEditingStyleDelete)
{
NSMutableArray *subArray = _dataArray[indexPath.section];
[subArray removeObjectAtIndex:indexPath.row];
}
if(editingStyle == UITableViewCellEditingStyleInsert)
{
NSLog(@"插入");
DeleteModel *model = _dataArray[indexPath.section][indexPath.row];
model.data = @"新数据";
NSMutableArray *subArray = _dataArray[indexPath.section];
[subArray insertObject:model atIndex:indexPath.row];
}
[tableView reloadData];
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"删除";
}
#pragma mark - 移动单元格
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSMutableArray *subArray = _dataArray[sourceIndexPath.section];
DeleteModel *model = _dataArray[sourceIndexPath.section][sourceIndexPath.row];
[subArray removeObjectAtIndex:sourceIndexPath.row];
NSMutableArray *desArray = _dataArray[destinationIndexPath.section];
[desArray insertObject:model atIndex:destinationIndexPath.row];
}
#pragma mark - 设置每组中的单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
if(tableView != _table)
{
DeleteModel *model = _searchArray[indexPath.row];
cell.textLabel.text = model.data;
}
else
{
DeleteModel *model = _dataArray[indexPath.section][indexPath.row];
cell.textLabel.text = model.data;
}
//imageView(头像), detailTextLable(副标题),accessoryType(右侧按键位置),accessoryView,
UIImageView *view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
view.image = [UIImage imageNamed:@"image2"];
cell.backgroundView = view;
cell.imageView.image = [UIImage imageNamed:indexPath.row%2?@"image1":@"image3"];
cell.frame = CGRectMake(0, 0, 0, 0);// 默认的,并不能改变大小
cell.detailTextLabel.text = @"莱博余万";
// 设置右边箭头
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
// 代理方法,给每个 cell 设置不同的高
// 注意: cell 默认是44
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0)
{
return 60;
}
return 40;
}
#pragma mark - 选择与反选
// 选择
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DeleteModel *model = _dataArray[indexPath.section][indexPath.row];
if(_table.editing == YES)
{
model.isDelete = YES;
}
}
// 反选
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
DeleteModel *model = _dataArray[indexPath.section][indexPath.row];
model.isDelete = NO;
}
#pragma mark - 设置表头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
view.backgroundColor = [UIColor lightGrayColor];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 0, 320, 30)];
label.text = _arrayM[section];
label.textColor = [UIColor blackColor];
[view addSubview:label];
return view;
}
#pragma mark - 设置右边索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return _arrayM;
}
iOS-UITableView
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 实现协议 添加到view 实现委托方法 //cell 的数量 // 分组的数量 // 具体cell的返回 // 选...
- //联系人:石虎QQ: 1224614774昵称:嗡嘛呢叭咪哄 实现代理三部曲:一:遵守代理二:把tableVie...
- (转自https://my.oschina.net/u/2473136/blog/525912 仅供个人查阅资料用...
- UITableView基本使用方法 如果自学的人可以按照这个路径去学习 进入要点 1.首先Controller需要...