zoukankan      html  css  js  c++  java
  • 抽屉开关控制器

    1、实现UITableView类似抽屉开关控制器的功能

    2、使用NSUserDefaults实现cell中输入UIfield的保存功能

    //  RootViewController.h
    #import <UIKit/UIKit.h>

    @interface RootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
    @property(nonatomic,retain)NSString *first;
    @property(nonatomic,retain)NSString *second;
    @property(nonatomic,retain)NSString *third;

    @end

    //  RootViewController.m

    #import "RootViewController.h"
    #import "TableViewCell.h"
    #import "WriteTableViewCell.h"
    @interface RootViewController ()
    {
        UITableView*table;
        NSMutableArray*arr;
        NSMutableArray*dataSource;
        NSInteger count;
        NSInteger tag;
        WriteTableViewCell *cell;
        NSMutableDictionary *dic;
    }
    @property (strong, nonatomic) NSArray *sectionHeaderArr;
    @property (strong, nonatomic) NSArray *bodyArr;
    @property (strong, nonatomic) NSMutableArray *selectedTagArr;

    @end
    @implementation RootViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.sectionHeaderArr = @[@"header1",
                                 @"header2",
                                 @"header3",
                                 @"header4"];
        self.bodyArr =@[@[@"h1_body1",@"h1_body2",@"h1_body3"],
                        @[@"h2_body1",@"h2_body2",@"h2_body3"],
                        @[@"h3_body1",@"h3_body2",@"h3_body3"],
                        @[@"h4_body1",@"h4_body2",@"h4_body3"]];
        //抽屉开关控制器
        //mutableCopy  深拷贝  之后可以改变数组里面的
        self.selectedTagArr = @[@"NO",
                                @"NO",
                                @"NO",
                                @"NO"].mutableCopy;
       
        dic=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3",[NSNumber numberWithInt:4],@"4", nil];  
        table=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        table.delegate=self;
        table.dataSource=self;
        [table setEditing:YES];
        
        
        //new    This method is a combination of alloc and init.
        //两种方法都行  new  c语言用的多
        //[table setTableFooterView:UIView.new];

        //创建table FooterView新的视图(为空), 可消除单元格下面的横线
        [table setTableFooterView:[UIView new]];

        //所有单元格下面的横线消失
        [table setSeparatorStyle:UITableViewCellSeparatorStyleNone];

        
        //默认是不控制是否可以在多行编辑模式,同时选择
        //[table setAllowsMultipleSelectionDuringEditing:YES];
        //是否支持编辑
        //[table setEditing:NO];
        [self.view addSubview:table];
       
        
        [self tableHeaderViewData]; //创建header视图
        [self tableFooterViewData]; //创建footer视图
        
        
        NSArray *arr22=@[@"添加数据库",@"删除数据库"];
        for (int i=0; i<2; i++) {
            UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
            btn.backgroundColor=[UIColor cyanColor];
            [btn setTitle:arr22[i] forState:UIControlStateNormal];
            btn.frame=CGRectMake(0+110*i, 70, 100, 50);
            btn.tag=i;
            [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:btn];
        }
        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(220, 70, 200, 50)];
        label.backgroundColor=[UIColor cyanColor];
        [self.view addSubview:label];
        NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
        if([defaults objectForKey:@"test"])
        {
            label.text = @"这不是第一次运行";
        }
        else
        {
            label.text = @"这是第一次运行";
            //将键值对写入userDefaults中
            [defaults setObject:@"message" forKey:@"test"];
            //将usersDefaults存放在磁盘上 但是不是必须要进行的操作
            [defaults synchronize];
        }
    }
    -(void)pressBtn:(id)sender
    {
        UIButton *btn=(UIButton *)sender;
        NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
        if (btn.tag==0) {
            //添加userDefaults下键值对
            
            [userDefaults setObject:_first forKey:@"0"];
            [userDefaults setObject:_second forKey:@"1"];
            [userDefaults setObject:_third forKey:@"2"];
            [userDefaults setObject:dic forKey:@"dic"];

            [userDefaults synchronize];
            
            NSArray * arr1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            
            NSString * path = [arr1 objectAtIndex:0];
            
            NSLog(@"1111%@",path);
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"添加本地数据成功" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
        else
        {
            [userDefaults removeObjectForKey:@"0"];
            [userDefaults removeObjectForKey:@"1"];
            [userDefaults removeObjectForKey:@"2"];
            [userDefaults removeObjectForKey:@"test"];
            //因为数据读取 第二次就是从磁盘的plist文件读取 我们上面的代码只是删除沙盒路径下的plist文件的内容
            [userDefaults synchronize];
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"删除本地数据成功" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
            [alert show];
        }
    }


    // tableHeaderView定义的View, 常用来做顶部轮播图片


    -(void)tableHeaderViewData
    {
        UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
        image.backgroundColor = [UIColor greenColor];
        table.tableHeaderView=image;
    }
    // tableFooterView定义的View, 常用来做底部上拉加载更多


    -(void)tableFooterViewData
    {
        UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
        image.backgroundColor = [UIColor greenColor];
        table.tableFooterView=image;
    }



    //如果不选择   默认为1
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return [_sectionHeaderArr count];
    }

    //标题的高度
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 40;
    }

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UIButton *headerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [headerBtn setFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
        [headerBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [headerBtn setTitle:_sectionHeaderArr[section] forState:UIControlStateNormal ];
        [headerBtn setTag:section];
        [headerBtn addTarget:self action:@selector(headerClickAction:) forControlEvents:UIControlEventTouchUpInside];
        return headerBtn;
    }

    - (void)headerClickAction:(UIButton *)sender
    {
         tag = sender.tag;
        //boolValue   bool数值
        BOOL isSelected = [_selectedTagArr[tag] boolValue];


        //更改开关控制器


        [_selectedTagArr replaceObjectAtIndex:tag withObject:@(!isSelected)];
        [table reloadData];
    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        //点击 header 按钮 是否显示抽屉
        if ([_selectedTagArr[section] boolValue])
            return [_bodyArr[section] count];
        else
            return 0;
    }
    -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //这样简化即可
       // TableViewCell*cell=[[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:self options:nil] lastObject];
        if (indexPath.section==1) {
        cell =[[[NSBundle mainBundle]loadNibNamed:@"WriteTableViewCell" owner:self options:nil] lastObject];
        //cell.textLabel.text= _bodyArr[indexPath.section][indexPath.row];
        cell.titleText.text=_bodyArr[indexPath.section][indexPath.row];
        cell.textField.tag=indexPath.row+10;
            [cell.textField addTarget:self action:@selector(textFieldWithText:) forControlEvents:UIControlEventEditingChanged];
        return cell;
        }
        else if (indexPath.section==0)
        {
        
            cell =[[[NSBundle mainBundle]loadNibNamed:@"WriteTableViewCell" owner:self options:nil] lastObject];
            //cell.textLabel.text= _bodyArr[indexPath.section][indexPath.row];
            cell.textField.tag=indexPath.row+20;
            NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
            NSString*number=[[NSString alloc]initWithFormat:@"%ld",(long)cell.textField.tag];
            
            if ([number isEqualToString:@"20"]) {
                cell.textField.text=[defaults objectForKey:@"0"];
            }
            else if ([number isEqualToString:@"21"]){
                cell.textField.text=[defaults objectForKey:@"1"];
            }
            else
            {
                cell.textField.text=[defaults objectForKey:@"2"];
            }

            [cell.textField addTarget:self action:@selector(textFieldWithText:) forControlEvents:UIControlEventEditingChanged];
            return cell;
        }
        else
        {
        TableViewCell*cell1=[[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:self options:nil] lastObject];
            cell1.textLabel.text=_bodyArr[indexPath.section][indexPath.row];
            return  cell1;
        }
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    - (void)textFieldWithText:(UITextField *)textField
    {
        
        switch (textField.tag) {
            case 10:
                _first = textField.text;
                NSLog(@"_first=%@",_first);
                break;
            case 11:
                _second = textField.text;
                NSLog(@"_second=%@",_second);
                break;
            case 12:
                _third = textField.text;
                NSLog(@"_third=%@",_third);
                break;
            case 20:
                _first = textField.text;
                NSLog(@"_first=%@",_first);
                break;
            case 21:
                _second = textField.text;
                NSLog(@"_second=%@",_second);
                break;
            case 22:
                _third = textField.text;
                NSLog(@"_third=%@",_third);
            default:
                break;
        }
    }

  • 相关阅读:
    电路原理图分析
    GPIO学习——用户空间操作
    在Android上运行Java和C程序
    Android命令行工具学习总结
    Android蓝牙学习笔记
    33 把数组排成最小的数
    233 Number of Digit One
    32 从1到n整数中1出现的次数
    31 连续子数组的最大和
    《大型网站技术架构》学习笔记
  • 原文地址:https://www.cnblogs.com/sayimba/p/5660419.html
Copyright © 2011-2022 走看看