代码改变世界

iOS YSMine 通用设置

  jiangys  阅读(645)  评论(0编辑  收藏  举报

概述

我们在开发的过程中,经常需要重复的写个人中心设置的代码,很多情况下,我们都是通过if(indexPath.row)来判断操作以及要跳转的页面,这样的情况是非常不友好的,尤其是当我们需要调整显示顺序、给点击添加处理等。

通用设置部分界面如下:

   

项目说明

该通用代码,我直接写在了YSKit(点击下载),项目结构如图

 

类说明:

我们在使用的时候,可以通过两种方式,一种是直接继承于YSMineViewController,另一种是使用数据源方式YSMineCellDataSource

承于YSMineViewController

这种方式简单,本身就是一个tableViewController了,直接设置数据源即可,如:

复制代码
#import "YSMineDemoViewController.h"
#import "YSMine.h"
#import "YSMineSettingViewController.h"

@implementation YSMineDemoViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupGroups];
}

/**
 *  初始化模型数据
 */
- (void)setupGroups
{
    [self setupGroup0];
    [self setupGroup1];
}

- (void)setupGroup0
{
    // 1.创建组
    YSMineCellGroup *group = [YSMineCellGroup group];
    group.header = @"这是第一组头部";
    group.footer = @"这是第一组底部";
    [self.groups addObject:group];
    
    // 2.设置组的所有行数据
    YSMineCellItemArrow *newFriend = [YSMineCellItemArrow itemWithTitle:@"帐号管理"];
    newFriend.destVcClass = [YSMineSettingViewController class];
    
    YSMineCellItemArrow *securityAccount = [YSMineCellItemArrow itemWithTitle:@"帐号安全"];
    securityAccount.destVcClass = [YSMineSettingViewController class];
    
    group.items = @[newFriend,securityAccount];
}

- (void)setupGroup1
{
    // 1.创建组
    YSMineCellGroup *group = [YSMineCellGroup group];
    group.header = @"这是第二组";
    [self.groups addObject:group];
    
    // 2.设置组的所有行数据
    YSMineCellItemArrow *theme = [YSMineCellItemArrow itemWithTitle:@"主题、背景"];
    theme.destVcClass = [YSMineSettingViewController class];
    
    YSMineCellItemArrow *notification = [YSMineCellItemArrow itemWithTitle:@"通知"];
    notification.destVcClass = [YSMineSettingViewController class];
    
    group.items = @[theme,notification];
}

@end
复制代码

YSMineCellDataSource数据源方式

使用数据源方式,是因为当前ViewController并不是tableViewController,可能还嵌套着其它页面。因此相对来说设置就会多一步,注意红色框的,设置数据源是一样的。

部分使用代码说明

使用很简单,所有的操作都是设置数据源的时候定义好了

复制代码
- (void)setupGroup0
{
    // 1.创建组
    YSMineCellGroup *group = [YSMineCellGroup group];
    group.header = @"这是第一组头部";
    group.footer = @"这是第一组底部";
    [self.groups addObject:group];
    
    // 2.设置组的所有行数据
    YSMineCellItemArrow *accountManage = [YSMineCellItemArrow itemWithTitle:@"帐号管理"]; // 右边显示的是箭头
    accountManage.destVcClass = [YSMineSettingViewController class]; // 设置点击该Cell要跳转的页面
    accountManage.operation = ^(){  // 需要执行的Block
        // 需要执行的代码
    };
    
    YSMineCellItemSwitch *notifition = [YSMineCellItemSwitch itemWithTitle:@"显示通知"]; // 右边显示的是开头
    
    YSMineCellItemLabel *contactUs = [YSMineCellItemLabel itemWithTitle:@"联系我们"]; // 右边显示的是文本
    contactUs.text = @"QQ:123456"; // 设置右边显示的文件
    
    group.items = @[accountManage,notifition,contactUs];
}
复制代码

上面代码的效果

 

源代码下载:点击下载

 

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示