iOS-UITableView

-(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)