一:在ios项目实际开发中经常会看到级联菜单的效果:如图:点击左侧菜单,右侧菜单刷新数据。此篇用两个tableView来实现如图效果:

二:代码:

1:构造数据模型:利用kvc快速构建数据模型

 #import <Foundation/Foundation.h>

 @interface XMGCategory : NSObject
/** 子类别 */
@property (nonatomic, strong) NSArray *subcategories;
/** 姓名 */
@property (nonatomic, strong) NSString *name;
/** 图标 */
@property (nonatomic, strong) NSString *icon;
/** 高亮图标 */
@property (nonatomic, strong) NSString *highlighted_icon; + (instancetype)categoryWithDict:(NSDictionary *)dict;
@end
 #import "XMGCategory.h"

 @implementation XMGCategory
+ (instancetype)categoryWithDict:(NSDictionary *)dict
{
XMGCategory *c = [[self alloc] init];
[c setValuesForKeysWithDictionary:dict];
return c;
}
@end

2:控制器代码实现:

 #import "ViewController.h"
#import "XMGCategory.h" @interface ViewController () <UITableViewDataSource, UITableViewDelegate>
/** 右边表格 */
@property (weak, nonatomic) IBOutlet UITableView *subcategoryTableView;
/** 左边表格 */
@property (weak, nonatomic) IBOutlet UITableView *categoryTableView;
/** 所有的类别数据 */
@property (nonatomic, strong) NSArray *categories;
@end @implementation ViewController #pragma mark -- 懒加载数据源
- (NSArray *)categories
{
if (!_categories) {
//1:从plist文件中读取数据
NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"categories" ofType:@"plist"]];
//2:构造数据源
NSMutableArray *categoryArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
[categoryArray addObject:[XMGCategory categoryWithDict:dict]];
} //3:赋值数据源
_categories = categoryArray;
}
return _categories;
} - (void)viewDidLoad {
[super viewDidLoad]; //1:默认选中左边表格的第0行
[self.categoryTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow: inSection:] animated:NO scrollPosition:UITableViewScrollPositionTop]; //2:给右侧的tableView增加额外的滚动区域:在有导航栏的时候,系统默认会为UIScrollView或是继承它的子控件默认增加64的额外滚动区域,如果有两个继承于UIScrollView的子控件,则系统默认只会为第一个添加到视图上的子控件增加额外的滚动区域。如果想禁止,则实现UIScrollView或是tableView等的ContentInset属性,增加额外的滚动区域,或是在控制器中实现self.automaticallyAdjustsScrollViewInsets = NO;
self.subcategoryTableView.contentInset = UIEdgeInsetsMake(, , , );
} - (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated]; NSLog(@"categoryTableView - %@", NSStringFromUIEdgeInsets(self.categoryTableView.contentInset));
NSLog(@"subcategoryTableView - %@", NSStringFromUIEdgeInsets(self.subcategoryTableView.contentInset));
} #pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// 左边表格
if (tableView == self.categoryTableView) return self.categories.count; // 右边表格
XMGCategory *c = self.categories[self.categoryTableView.indexPathForSelectedRow.row];
return c.subcategories.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 左边表格
if (tableView == self.categoryTableView) {
static NSString *ID = @"category";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; XMGCategory *c = self.categories[indexPath.row]; // 设置普通图片
cell.imageView.image = [UIImage imageNamed:c.icon];
// 设置高亮图片(cell选中 -> cell.imageView.highlighted = YES -> cell.imageView显示highlightedImage这个图片)
cell.imageView.highlightedImage = [UIImage imageNamed:c.highlighted_icon]; // 设置label高亮时的文字颜色
cell.textLabel.highlightedTextColor = [UIColor redColor]; cell.textLabel.text = c.name;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell;
} else {
// 右边表格
static NSString *ID = @"subcategory";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 获得左边表格被选中的模型
XMGCategory *c = self.categories[self.categoryTableView.indexPathForSelectedRow.row];
cell.textLabel.text = c.subcategories[indexPath.row]; return cell;
}
} #pragma mark - <UITableViewDelegate>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.categoryTableView) {
[self.subcategoryTableView reloadData];
}
}

三:知识点总结:

1:级联菜单数据模型的设计:1:左侧表格数据模型中含有右侧表格的数据模型,在定义数据模型时,可用类方法,或是实例方法:类方法实现:alloc创建对象,对象调用KVC快速为属性赋值,利用KVC必须满足属性一一对应,不能少也不多

+ (instancetype)categoryWithDict:(NSDictionary *)dict

{

XMGCategory *c = [[self alloc] init];

[c setValuesForKeysWithDictionary:dict];

return c;

}

2:在构造数据源时:要看清plist根节点是数组还是字典:一般自定义plist数据模型时时,最外层为数组,数组为每一个数据模型的字典:数据源,采用懒加载,懒加载保证只初始化一次,且不用关心何时创建也就是不用考虑代码顺序。从plist中读取数据:

NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"categories" ofType:@"plist"]];

因为懒加载时,数据源本身就没有初始化,不存在,所以用

_categories = categoryArray;来对数据源进行赋值,若是初始化了,则可以addObjectFromeArray,或是插入数据:insetObject  atIndexSets

3:tableView默认选中某一行:

//1:默认选中左边表格的第0行

[self.categoryTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];

4:给UIscrollView,tableView,UICollectionView增加额外的滚动区域调用:contentInset.

给右侧的tableView增加额外的滚动区域:在有导航栏的时候,系统默认会为UIScrollView或是继承它的子控件默认增加64的额外滚动区域,如果有两个继承于UIScrollView的子控件,则系统默认只会为第一个添加到视图上的子控件增加额外的滚动区域。如果想禁止,则实现UIScrollView或是tableView等的ContentInset属性,增加额外的滚动区域,或是在控制器中实现self.automaticallyAdjustsScrollViewInsets = NO;

self.subcategoryTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);

5:在方法

- (void)viewDidAppear:(BOOL)animated里视图已经出现,在此方法中,也就是视图出现的时候,能正确打印,调试,或是对控件进行一些设置

6:两个tableView,在实现数据源或是代理方法时,要区分不同的tableView:如:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

// 左边表格

if (tableView == self.categoryTableView) return self.categories.count;

// 右边表格

XMGCategory *c = self.categories[self.categoryTableView.indexPathForSelectedRow.row];

return c.subcategories.count;

}

其中,self.categoryTableView.indexPathForSelectedRow,得到的是当前表格选中的indexPath区域

7:UITableViewCell:当某个cell被选中时,系统会默认显示cell中控件高亮时的状态,取消选中时,显示常态:可以设置cell中的UIimageView,lable高亮时的状态:

//1: 设置普通图片

cell.imageView.image = [UIImage imageNamed:c.icon];

// 设置高亮图片(cell选中 -> cell.imageView.highlighted = YES -> cell.imageView显示highlightedImage这个图片)

cell.imageView.highlightedImage = [UIImage imageNamed:c.highlighted_icon];

// 2:设置label高亮时的文字颜色

cell.textLabel.highlightedTextColor = [UIColor redColor];

												

ios开发之级联菜单(两个tableView实现)的更多相关文章

  1. iOS开发UI篇—推荐两个好用的Xcode插件(提供下载链接)

    iOS开发UI篇—推荐两个好用的Xcode插件(提供下载链接) 这里推荐两款好用的Xcode插件,并提供下载链接. 一.插件和使用如下: 1.两款插件 对项目中图片提供自动提示功能的插件:KSImag ...

  2. iOS 类似美团外卖 app 两个 tableView 联动效果实现

    写在前面 首先声明哈,不是广告,我就是用的时候觉得这个功能比较好玩,就想着实现了一下.效果如图: 接下来简单的说一下思路吧~ 大体思路 可能我们看到这种功能的实现的时候,首先想着的是我在这个控制器中左 ...

  3. IOS开发复习笔记(4)-TableView

    总结几个TableView常用的代码 1.初始化方面 static string CellIndetifier=@"cellIndetifier"; -(NSInteger)num ...

  4. ---iOS开发 截取字符串中两个指定字符串中间的字符串---

    例如,要截取一个字符串中,两个指定字符串中间的字符串,OC截取方法如下: // 要截取 "> 和 </ 之间的汉字内容: @implementationViewControlle ...

  5. iOS开发NSFetchedResultsController的使用CoreData和TableView数据同步更新

    1.效果 2.代码 #import "ViewController.h" #import "Student+CoreDataProperties.h" #def ...

  6. iOS开发之转盘菜单

    前言 使用Swift实现的转盘菜单,主要用到UIBezierPath.CALayer遮罩绘制扇形UIView,CATransform3DMakeRotation实现旋转动画.代码设计使用默认confi ...

  7. iOS 类似外卖 两个tableView联动

    在伯乐在线上看到一个挺好玩的文章,自己也参考文章实现了一下. 效果实现如图所示: 具体实现的内容可以参考原文,参考文章:<iOS 类似美团外卖 app 两个 tableView 联动效果实现&g ...

  8. iOS开发小技巧 -- tableView-section圆角边框解决方案

    [iOS开发]tableView-section圆角边框解决方案 tableView圆角边框解决方案 iOS 7之前,图下圆角边框很容易设置 iOS 7之后,tableviewcell的风格不再是圆角 ...

  9. 文顶顶iOS开发博客链接整理及部分项目源代码下载

    文顶顶iOS开发博客链接整理及部分项目源代码下载   网上的iOS开发的教程很多,但是像cnblogs博主文顶顶的博客这样内容图文并茂,代码齐全,示例经典,原理也有阐述,覆盖面宽广,自成系统的系列教程 ...

随机推荐

  1. Red Hat Enterprise Linux Server 5.5新增功能简介

    一.概况 昨日对刚发布的redhat新产品RHEL5.5进行了小范围的测试,红帽企业版 Linux 5.5 发行本中的亮点包括针对 Intel Boxboro-EX 平台.AMD Magny-Cour ...

  2. error: function declaration isn’t a prototype [-Werror=strict-prototypes]

    "warning: function declaration isn't a prototype" was caused by the function like that:   ...

  3. Emmet学习教程

    Emmet (前身为 Zen Coding) 是一个能大幅度提高前端开发效率的一个工具,Emmet是很成熟的并且非常适用于编写HTML/XML 和 CSS 代码的前端开发人员,但也可以用于编程语言.所 ...

  4. 阅读笔记——Web应用程序

    Web应用程序与DD文件 Web应用程序 web应用程序是一种可以通过Web访问的应用程序.Web应用程序最大的好处是永和很容易访问应用程序.用户只需要有浏览器即可,不需要安装其他任何软件.一个Web ...

  5. C++访问WebService gSoap方式

    一.             gSOAP访问WebService 1.      下载gSOAP gSOAP 2.7.17 版下载地址http://sourceforge.net/projects/g ...

  6. 配置mysql的ODBC数据源

    如果你已经安装好了mysql和mysql连接驱动,则可以向下进行了 打开控制面板,以小图标的形式查看,找到管理工具 打开管理工具,找到数据源(odbc),打开 在图片中所圈出的三个标签中随便选一个,点 ...

  7. mysql测试spring事务是否生效

    同时对三张表进行插入操作,事务保证完整性.下面进行简单测试: 1. 锁定表 锁定用户表 LOCK TABLES user WRITE; 查看表是否锁定: show ; 显示被锁定的表. 2. 验证在同 ...

  8. ORACLE10g R2【RAC+ASM→单实例FS】

    ORACLE10g R2[RAC+ASM→单实例FS] 10g R2 RAC+ASMà单实例FS的DG,建议禁用OMF. 本演示案例所用环境:   primary standby OS Hostnam ...

  9. 微信小程序图片使用示例

    小程序官方API:https://developers.weixin.qq.com/miniprogram/dev/component/image.html 1:加载本地文件夹图片 <image ...

  10. H5移动端IOS/Android兼容性总结,持续更新中…

    H5移动端IOS/Android兼容性总结,持续更新中… 1. IOS不识别日期 new Date("2018-07-01 08:00:00")在Android下正常显示可以直接进 ...