码迷,mamicode.com
首页 > 其他好文 > 详细

三级展开框的制作 未仔细测试有无bug

时间:2015-11-14 14:56:52      阅读:428      评论:0      收藏:0      [点我收藏+]

标签:

1. .m内容

#import "XLTableView.h"
#import "XLTableViewCell.h"
#import "PowerPictureViewController.h"

@interface XLTableView ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate,UIAlertViewDelegate>


@property (nonatomic ,strong) NSMutableArray *Source_Array;

@property (nonatomic ,strong) NSMutableArray *redundant_Source_Array;

@property (nonatomic ,strong) NSMutableArray *remove_Data_Path_Array;

@property (nonatomic ,strong) NSMutableArray *redundant_Source_Array_total;

@property (nonatomic ,strong) NSMutableArray *remove_Data_Path_Array_total;

@property (nonatomic ,strong) NSArray *source_arr;

@property (nonatomic ,assign) NSInteger Path;

@property (nonatomic ,assign) NSInteger path_Path;


@property (nonatomic ,strong) NSString *The_first_layer_kay;//第一层键值

@property (nonatomic ,strong) NSString *The_second_floo_kay;//第二层键值

@property (nonatomic ,strong) NSString *The_third_layer_kay;//第三层键值

@end

@implementation XLTableView

- (instancetype)init_With_Frame:(CGRect)frame
                      with_Data:(NSArray *)Sourec_Array
            The_first_layer_kay:(NSString *)The_first_layer_kay
            The_second_floo_kay:(NSString *)The_second_floo_kay
            The_third_layer_kay:(NSString *)The_third_layer_kay
{
    self = [super initWithFrame:frame style:UITableViewStyleGrouped];
    
    if (self) {
        
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [self registerNib:[UINib nibWithNibName:@"XLTableViewCell" bundle:nil]  forCellReuseIdentifier:@"XL_table_View_Cell"];
        
        self.path_Path = 0;
        
        self.dataSource = self;
        
        self.delegate = self;
        
        self.The_first_layer_kay = The_first_layer_kay;
        
        self.The_second_floo_kay = The_second_floo_kay;
        
        self.The_third_layer_kay = The_third_layer_kay;
        
        self.index_Path_Array = [NSMutableArray array];
        
        self.redundant_Source_Array = [NSMutableArray array];
        
        self.remove_Data_Path_Array = [NSMutableArray array];
        
        self.redundant_Source_Array_total = [NSMutableArray array];
        
        self.remove_Data_Path_Array_total = [NSMutableArray array];
        
        self.Source_Array = [self create_Temp_Data:Sourec_Array];
        
        self.all_Source_Array = [self Remove_all_levels:self.Source_Array];
    }
    return self;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    Header_View = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 45)];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                          action:@selector(tap:)];
    
    Header_View.backgroundColor = [UIColor whiteColor];
   
    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tianjia.png"]];
    
    image.frame = CGRectMake(0,0, 25, 25);
    
    image.center = Header_View.center;
    
    [Header_View addSubview:image];
    
    [Header_View addGestureRecognizer:tap];
    
    UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, self.frame.size.width, 1)];
    
    lable.backgroundColor = [UIColor grayColor];
    
    [Header_View addSubview:lable];
    
    return Header_View;
}
-(void )tap:(UITapGestureRecognizer *)tap
{
    NSArray *other_Button_Titles = @[@"添加",@"修改"];
    if (!self.is) {
        other_Button_Titles = @[@"添加"];
    }
    [self createAlert_View_message:@"添加或修改"
                             Title:@"提示"
               cancel_Button_Title:@"取消"
               other_Button_Titles:other_Button_Titles
                         alert_Tag:AlertTag];
}
- (void)createAlert_View_message:(NSString *)message
                           Title:(NSString *)Title
             cancel_Button_Title:(NSString *)cancel_Button_Title
             other_Button_Titles:(NSArray *)other_Button_Titles
                       alert_Tag:(NSUInteger)tag
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:Title
                                                    message:message
                                                   delegate:self.vc
                                          cancelButtonTitle:cancel_Button_Title
                                          otherButtonTitles: nil];
    
    for (int32_t i = 0; i < other_Button_Titles.count; i++) {
        
        [alert addButtonWithTitle:other_Button_Titles[i]];
    }
    
    alert.tag = tag;
    
    [alert show];
}
- (void)createAlert_View_message:(NSString *)message
                           Title:(NSString *)Title
             cancel_Button_Title:(NSString *)cancel_Button_Title
             other_Button_Titles:(NSArray *)other_Button_Titles
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:Title
                                                    message:message
                                                   delegate:self
                                          cancelButtonTitle:cancel_Button_Title
                                          otherButtonTitles: nil];
    for (int32_t i = 0; i < other_Button_Titles.count; i++) {
        [alert addButtonWithTitle:other_Button_Titles[i]];
    }
    
    alert.tag = AlertTag;
    
    [alert show];
}
-(NSMutableArray *)create_Temp_Data:(NSArray *)Data{
    
    NSMutableArray *temp_Array = [NSMutableArray array];
    
    for (int i = 0; i < Data.count; i++) {
        
            [temp_Array addObject:Data[i]];
        
    }
    
    return temp_Array;
}

- (NSMutableArray *)Remove_all_levels:(NSMutableArray *)Source_Array
{
    NSMutableArray *all_Array = [NSMutableArray array];
    
    NSArray *The_first_layer_Array;
    
    NSArray *The_third_layer_Array;
    
    for (int32_t i = 0; i < Source_Array.count; i++) {
        
        NSArray *arr = @[@"1",@"1"];
        
        if (!self.The_first_layer_kay) {
            
            [all_Array addObject:Source_Array[i]];
            
        }else
        {
            [all_Array addObject:[Source_Array[i] objectForKey:self.The_first_layer_kay]];
            
            The_first_layer_Array = [[Source_Array[i] objectForKey:self.The_first_layer_kay] objectForKey:self.The_second_floo_kay];
            
            if (!The_first_layer_Array.count) {
                
                arr = @[@"1",@"222"];
            }
        }
        [self.index_Path_Array addObject:arr];
        
        for (int32_t j = 0; j < The_first_layer_Array.count; j ++) {
            
            NSArray *arr = @[@"2",@"1"];
            
            if (!self.The_first_layer_kay) {
                
                [all_Array addObject:The_first_layer_Array[j]];
                
                The_third_layer_Array = [The_first_layer_Array[j] objectForKey:self.The_third_layer_kay];
                
            }else
            {
                [all_Array addObject:The_first_layer_Array[j]];
                
                The_third_layer_Array = [The_first_layer_Array[j] objectForKey:self.The_third_layer_kay];
                
                if (!The_third_layer_Array.count) {
                    
                    arr = @[@"2",@"222"];
                }
            }
            [self.index_Path_Array addObject:arr];
            
            for (int32_t l = 0; l < The_third_layer_Array.count; l++) {
                
                 NSArray *arr = @[@"3",@"222"];
                
                [self.index_Path_Array addObject:arr];
                
                [all_Array addObject:The_third_layer_Array[l]];
            }
        }
    }
    return all_Array;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.all_Source_Array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier = @"XL_table_View_Cell";
    
    static XLTableViewCell *cell = nil;
    
    cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    
    if (!cell) {
        
        cell = [[XLTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:indentifier];
    }
    cell.text_Label.text = [self table_View_Cell_Index_Path:indexPath];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self did_Select_Row_Index_Path:indexPath];
}
- (NSString *)table_View_Cell_Index_Path:(NSIndexPath *)indexPath
{
    NSString *Cell_string;
    
    NSArray *arr = self.index_Path_Array[indexPath.row];
    
    switch ([arr[0] intValue]) {
        case 1:
            Cell_string = [NSString stringWithFormat:@"%@",
                           [self.all_Source_Array[indexPath.row] objectForKey:@"name"]];
            break;
        case 2:
            Cell_string = [NSString stringWithFormat:@"       %@",
                           [self.all_Source_Array[indexPath.row] objectForKey:@"name"]];
            break;
        case 3:
           Cell_string = [NSString stringWithFormat:@"              %@",
                          [self.all_Source_Array[indexPath.row] objectForKey:@"name"]];
            break;
        default:
            break;
    }
    return Cell_string;
}
- (void)did_Select_Row_Index_Path:(NSIndexPath *)indexPath
{
    NSArray *arr;
    
    arr = self.index_Path_Array[indexPath.row];
    
    if (![arr[0] isEqualToString:@"3"]) {
        
        if ([arr[1] intValue] == 1){
            
            arr = @[arr[0],@"0"];
            
            [self.index_Path_Array replaceObjectAtIndex:indexPath.row
                                             withObject:arr];
            
            [self remove_data_View_Cell_data:indexPath
                                       Class:arr[0]];
            
        }else if ([arr[1] intValue] == 0) {
            
            arr = @[arr[0],@"1"];
            
            [self.index_Path_Array replaceObjectAtIndex:indexPath.row
                                             withObject:arr];
            
            [self add_table_view_cell_data:indexPath.row
                    redundant_Source_Array:self.redundant_Source_Array
                    remove_Data_Path_Array:self.remove_Data_Path_Array];
            
            [self cell_The_starting_position:indexPath.row
                                end_Position:self.redundant_Source_Array.count
                                     isState:YES];
        }
    }
}
- (void)add_table_view_cell_data:(NSInteger)indexPath_row
          redundant_Source_Array:(NSMutableArray *)redundant_Source_Array
          remove_Data_Path_Array:(NSMutableArray *)remove_Data_Path_Array
{
    for (int32_t i = 0; i < redundant_Source_Array.count; i++) {
        
        NSDictionary *dic = redundant_Source_Array[i];
        
        NSArray *Path_Dic = remove_Data_Path_Array[i];
        
        [self.all_Source_Array insertObject:dic
                                    atIndex:indexPath_row+1+i];
        
        [self.index_Path_Array insertObject:Path_Dic
                                    atIndex:indexPath_row+1+i];
    }
}
- (void)remove_data_View_Cell_data:(NSIndexPath *)indexPath
                             Class:(NSString *)string
{
    NSArray *source_arr1;
    
    source_arr1 = self.index_Path_Array[self.Path];
    
    if (self.redundant_Source_Array.count
        && self.Path != indexPath.row
        && [source_arr1[1] intValue] == 0
        && self.Path > indexPath.row) {
        
        source_arr1 = @[source_arr1[0],@"1"];
        
        [self.index_Path_Array replaceObjectAtIndex:self.Path
                                         withObject:source_arr1];
        
        [self add_table_view_cell_data:self.Path
                redundant_Source_Array:self.redundant_Source_Array
                remove_Data_Path_Array:self.remove_Data_Path_Array];
        
        [self cell_The_starting_position:self.Path
                            end_Position:self.redundant_Source_Array.count
                                 isState:YES];
    }
    if (self.redundant_Source_Array.count
        && self.Path != indexPath.row
        && [source_arr1[1] intValue] == 0
        && self.Path < indexPath.row) {
        
        for (int32_t i = 0; i < self.redundant_Source_Array.count; i++) {
            
            [self.redundant_Source_Array_total addObject:self.redundant_Source_Array[i]];
            
            [self.remove_Data_Path_Array_total addObject:self.remove_Data_Path_Array[i]];
        }
    }else if(self.Path > indexPath.row)
        self.path_Path = 0;
    
    [self.redundant_Source_Array removeAllObjects];
    
    [self.remove_Data_Path_Array removeAllObjects];
    
    for (int32_t i = (int32_t)indexPath.row+1; i < self.index_Path_Array.count; i++) {
        
        NSArray *arr = self.index_Path_Array[i];
        
        if ([arr[0] isEqualToString:string]) {
            break;
        }else if ([string isEqualToString:@"2"]) {
            
            if ([arr[0] isEqualToString:@"1"]) {
                break;
            }else {
                [self.redundant_Source_Array addObject:self.all_Source_Array[i]];
                [self.remove_Data_Path_Array addObject:self.index_Path_Array[i]];
            }
        }else {
            [self.redundant_Source_Array addObject:self.all_Source_Array[i]];
            [self.remove_Data_Path_Array addObject:self.index_Path_Array[i]];
        }
    }
    [self.all_Source_Array removeObjectsInRange:NSMakeRange(indexPath.row+1, self.redundant_Source_Array.count)];
    
    [self.index_Path_Array removeObjectsInRange:NSMakeRange(indexPath.row+1, self.remove_Data_Path_Array.count)];
    
    [self cell_The_starting_position:indexPath.row
                        end_Position:self.redundant_Source_Array.count
                             isState:NO];

    if (self.redundant_Source_Array_total.count) {
        
         source_arr1 = @[source_arr1[0],@"1"];
        
        [self.index_Path_Array replaceObjectAtIndex:self.Path withObject:source_arr1];

        [self add_table_view_cell_data:self.Path
                redundant_Source_Array:self.redundant_Source_Array_total
                remove_Data_Path_Array:self.remove_Data_Path_Array_total];
        
        [self cell_The_starting_position:self.Path
                            end_Position:self.redundant_Source_Array_total.count
                                 isState:YES];
        
        self.path_Path = self.redundant_Source_Array_total.count;
        
        [self.redundant_Source_Array_total removeAllObjects];
        
        [self.remove_Data_Path_Array_total removeAllObjects];
    }
    
    self.Path = indexPath.row + self.path_Path;
}
- (void)cell_The_starting_position:(NSInteger)start
                      end_Position:(NSInteger)termination
                           isState:(BOOL)state
{
    NSMutableArray *arr = [NSMutableArray array];
    
    for (NSInteger i = start+1; i <start+termination+1 ; i++) {
        
        NSIndexPath *index_path = [NSIndexPath indexPathForRow:i inSection:0];
        
        [arr addObject:index_path];
    }
    if (state) {
        [self insertRowsAtIndexPaths:arr
                    withRowAnimation:UITableViewRowAnimationNone];
    }else
        [self deleteRowsAtIndexPaths:arr
                    withRowAnimation:UITableViewRowAnimationNone];
}

@end

 2. .h 内容

#import <UIKit/UIKit.h>

@protocol XLTableViewDelegate <NSObject>
@end

@interface XLTableView : UITableView
{
    UIView *Header_View;
}

- (instancetype)init_With_Frame:(CGRect)frame
                      with_Data:(NSArray *)Sourec_Array
            The_first_layer_kay:(NSString *)The_first_layer_kay
            The_second_floo_kay:(NSString *)The_second_floo_kay
            The_third_layer_kay:(NSString *)The_third_layer_kay;


@property (nonatomic ,strong) NSMutableArray *index_Path_Array;

@property (nonatomic ,strong) NSMutableArray *all_Source_Array;

@property (nonatomic ,strong) UIViewController *vc;

@property (nonatomic ,assign) BOOL is;


@end

 

三级展开框的制作 未仔细测试有无bug

标签:

原文地址:http://www.cnblogs.com/xiaoxiaolei/p/4964335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!