zoukankan      html  css  js  c++  java
  • iOS开发之--简单的二级联动页面

    1、文件目录

     2、默认选中第0个,然后左侧菜单栏点击切换分类,右侧刷新数据,很简单,这里做个记录

    默认选中代码:

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
        [super setSelected:selected animated:animated];
        self.leftView.hidden = selected == YES ? NO:YES;
        self.lab.textColor = selected == YES ? UIColor.whiteColor:UIColor.lightGrayColor;
        self.lab.backgroundColor = selected == YES ? UIColor.blackColor : UIColor.whiteColor;
        
        // Configure the view for the selected state
    }

    3、思路狠简单,解析数据,转数据模型,然后传递相应model即可,具体代码如下:

    #define Window_Bounds   [UIScreen mainScreen].bounds
    #define Window_H        [[UIScreen mainScreen] bounds].size.height
    #define Window_W        [[UIScreen mainScreen] bounds].size.width
    
    #import "ViewController.h"
    #import "leftCell.h"
    #import "rightCell.h"
    #import "Model.h"
    #import "MJExtension.h"
    #import "NSString+MJExtension.h"
    #import "NSObject+MJClass.h"
    #import "subModel.h"
    
    @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
    {
        UITableView *_leftTableV;
        UITableView *_rightTableV;
        NSIndexPath *_selIndexPath;
        rightCell *cell;
    }
    
    @property (nonatomic, strong) NSMutableArray *dataAry,*subcategories;
    
    @end
    
    @implementation ViewController
    
    #pragma mark -- 懒加载数据源
    - (NSMutableArray *)dataAry
    {
      if (!_dataAry) {
          _dataAry = [NSMutableArray array];
          //获取文件路径
          NSString *filePath = [[NSBundle mainBundle] pathForResource:@"text" ofType:@"json"];
          //获取文件内容
          NSString *jsonStr  = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
          //将文件内容转成数据
          NSData *jaonData   = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
          //将数据转成数组
          NSDictionary *dataDcit = [NSJSONSerialization JSONObjectWithData:jaonData options:NSJSONReadingMutableContainers error:nil];
          NSMutableArray *cateAry = [NSMutableArray arrayWithArray:dataDcit[@"data"][@"categories"]];
          NSLog(@"cateAry is %@",cateAry);
          for (NSDictionary *dict in cateAry) {
              Model *model = [Model mj_objectWithKeyValues:dict];
              [_dataAry addObject:model];
          }
      }
      return _dataAry;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        _leftTableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 100, Window_H) style:UITableViewStylePlain];
        _leftTableV.delegate = self;
        _leftTableV.dataSource = self;
        _leftTableV.tableFooterView = [UIView new];
        [self.view addSubview:_leftTableV];
        
        [_leftTableV selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
    
        
        _rightTableV = [[UITableView alloc]initWithFrame:CGRectMake(100, 0, Window_W-100, Window_H) style:UITableViewStylePlain];
        _rightTableV.delegate = self;
        _rightTableV.dataSource = self;
        _rightTableV.tableFooterView = [UIView new];
        [self.view addSubview:_rightTableV];
    
    }
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        if (tableView == _leftTableV) {
            return self.dataAry.count;
        }
        
        return self.subcategories.count;
    }
    
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 50.0f;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (tableView == _leftTableV) {
            static NSString *left_cellIdentifier = @"leftCell";
            leftCell *cell = [_leftTableV dequeueReusableCellWithIdentifier:left_cellIdentifier];
            if (!cell) {
                cell = [[leftCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:left_cellIdentifier];
                cell.lab.text = [NSString stringWithFormat:@"%@",_dataAry[indexPath.row]];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
                    
            cell.model = self.dataAry[indexPath.row];
            
            return cell;
        }else{
            static NSString *right_cellIdentifier = @"rightCell";
            cell = [_rightTableV dequeueReusableCellWithIdentifier:right_cellIdentifier];
            if (!cell) {
                cell = [[rightCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:right_cellIdentifier];
            }
            
            cell.subModel = self.subcategories[indexPath.row];
                    
            return cell;
        }
    }
    
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        if (tableView == _leftTableV) {
            
            [self.subcategories removeAllObjects];
            
            //当前选中的索引
            _selIndexPath = indexPath;
            
            [self setSubData];
            
            [_rightTableV reloadData];
        }
    }
    
    
    -(NSMutableArray *)subcategories{
        if (!_subcategories) {
            //默认选中展示数据
            _subcategories = [NSMutableArray array];
            [self setSubData];
        }
        return  _subcategories;
    }
    
    -(void)setSubData{
        Model *model = [[Model alloc]init];
        model = self.dataAry[_selIndexPath.row];
        NSMutableArray *subAry = [NSMutableArray arrayWithArray:model.subcategories];
        for (NSDictionary *dict in subAry) {
            subModel *model = [subModel mj_objectWithKeyValues:dict];
            [_subcategories addObject:model];
        }
    }
    
    @end

    效果如下:

    仅做记录!

  • 相关阅读:
    SqlSelect记录搜索
    SqlSelect在查询结果前面加一列序号列
    关于MMC不能打开文件C:\Program Files\Microsoft SQL Server\80\Tools\Binn\SQL Server Enterprise Manager.MSC可能是由于文件不存在,不是一个MMC控制台,或者用后来的MMC版本创建。也可能你没有访问此文件的足够权限
    IsPostBack郁闷
    查询值的数目与目标字段中的数目不同
    SqlServer2000数据库备份
    三种antiTNF制剂生物学剂量增加、非生物制剂和类固醇强化作用的差异:来自临床实践的证据
    三种影像学方法证实依那西普治疗银屑病关节炎有关节修复作用
    北美地区早期和长期类风湿性关节炎患者应用依那西普10年以上的安全性和有效性
    肿瘤坏死因子(TNF)阻断剂治疗幼年型银屑病关节炎: 有效吗
  • 原文地址:https://www.cnblogs.com/hero11223/p/14153711.html
Copyright © 2011-2022 走看看