iOS开发之--简单的二级联动页面

iOS开发之--简单的二级联动页面

2023年7月13日发(作者:)

iOS开发之--简单的⼆级联动页⾯1、⽂件⽬录

2、默认选中第0个,然后左侧菜单栏点击切换分类,右侧刷新数据,很简单,这⾥做个记录默认选中代码:- (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; = selected == YES ? NO:YES; lor = selected == YES ? olor:rayColor; oundColor = selected == YES ? olor : olor;

// Configure the view for the selected state}3、思路狠简单,解析数据,转数据模型,然后传递相应model即可,具体代码如下:#define Window_Bounds [UIScreen mainScreen].bounds#define Window_H [[UIScreen mainScreen] bounds].#define Window_W [[UIScreen mainScreen] bounds].#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 (){ 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]; _te = self; _urce = self; _ooterView = [UIView new]; [ 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]; _te = self; _urce = self; _ooterView = [UIView new]; [ addSubview:_rightTableV];}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (tableView == _leftTableV) { return ; }

return ;}-(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]; = [NSString stringWithFormat:@"%@",_dataAry[]]; ionStyle = UITableViewCellSelectionStyleNone; }

= y[];

return cell; }else{ static NSString *right_cellIdentifier = @"rightCell"; cell = [_rightTableV dequeueReusableCellWithIdentifier:right_cellIdentifier]; if (!cell) { cell = [[rightCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:right_cellIdentifier]; }

el = egories[];

return cell; }}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (tableView == _leftTableV) {

[egories 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 = y[_]; NSMutableArray *subAry = [NSMutableArray arrayWithArray:egories]; for (NSDictionary *dict in subAry) { subModel *model = [subModel mj_objectWithKeyValues:dict]; [_subcategories addObject:model]; }}@end

效果如下:

仅做记录!

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1689263913a226396.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信