iOS 开发里使用MVC编程模式项目

本文介绍了一次针对红包模型的代码重构过程,提高了代码的可维护性和效率。通过对原有代码进行改造,使得模型更加清晰易懂,并实现了更好的数据加载与展示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上篇文章说了自己在公司代码遇到的问题,可维护性很低,做了很多无用功才熟悉代码。点击打开链接http://blog.youkuaiyun.com/a158337/article/details/50550941


今天我把原来的代码的核心部分都改了,和昨天实现的效果没有变,但是以后的可维护性一定更好了。

模型文件

#import <Foundation/Foundation.h>

/**
 *  速递红包模型
 
 /**
 *
 
 user_id
 page		页数(每页10个)
 
 返回示例:
 请求成功且有可用优惠券时:
 {"code":200,"message":"success","content":
 {"coupon_info":[
 {
 "coupon_id":"323",
 "is_used":"2",
 "source":"商城发放",
 "give_time":"2015.10.12",
 "coupon_price":"1.5",
 "available_time":"23小时后可用"
 },
 {"coupon_id":"25003",
 "is_used":"0",
 "source":"系统发放",
 "give_time":"2015.08.20 17:46",
 "expire_time":"2015.08.29 17:46",
 "coupon_price":"1.5"
 }
 ]
 }
 }
 请求成功但没有可用的优惠券时:
 {"code":200,"message":"success","content":{"coupon_info":null}}
 
 备注: is_used   0 为未使用, 2为不可用
 
 */

@interface ExpressRedPageModel : NSObject
/**
 *  红包id
 */
@property (nonatomic,strong) NSString *coupon_id;
/**
 *   0 为未使用, 2为不可用
 */
@property (nonatomic,strong) NSString* is_used;
/**
 *  来源
 */
@property(nonatomic,strong)NSString * source;
/**
 *  有效时间
 */
@property (nonatomic,strong) NSString*available_time;
/**
 *  发放时间
 */
@property (nonatomic,strong) NSString* give_time;
/**
 *  红包价格
 */
@property (nonatomic,strong) NSString *coupon_price;

/**
 *  过期时间,这里在is_used = 2的时候才会有这个字段
 */
@property (nonatomic,strong) NSString*expire_time;

/**
 *  是否被选择,这里是为了可合并使用的红包里面的多选和状态的切换
 */
@property (nonatomic,assign) BOOL isCombineSelected;
/**
 *  是否可以合并
 */
@property (nonatomic,assign) BOOL isCombined;
/**
 *  红包的背景
 */
@property (nonatomic,strong) NSString* backImage;
/**
 *  红包的展示模式,0 正常,1 显示为合并。
 */
@property (nonatomic,assign) NSInteger cellShowMode;

+(instancetype)hbWithDict:(NSDictionary*)dict;
-(instancetype)initWithDict:(NSDictionary*)dict;


@end
#import "ExpressRedPageModel.h"

@implementation ExpressRedPageModel
+(instancetype)hbWithDict:(NSDictionary*)dict
{
    return  [[self alloc]initWithDict:dict];
}
-(instancetype)initWithDict:(NSDictionary*)dict
{
    if (self = [super init]) {
        
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

@end

加载数据


- (void)loadData:(NSDictionary *)YHQDic
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    //图片列表
    imageList = [[NSArray alloc]initWithObjects:@"1.5+@2x.png",@"1.1_1.5@2x.png",@"0.6_1.0@2x.png",@"0_0.5@2x.png",@"0_0.0@2x.png", nil];
    NSArray *arr = YHQDic[@"coupon_info"];

    //上面刚刚 NSArray *arr = YHQDic[@"coupon_info"]; 下面循环依旧用YHQDic,仅仅就为了计算count
    for (NSInteger i = 0; i < arr.count; i ++) {
        
       ExpressRedPageModel * redModel = [[ExpressRedPageModel alloc]initWithDict:arr[i]];
        
        if ([redModel.is_used isEqualToString:@"2"]) {
            redModel.backImage=imageList[4];
        }
        else
        {
            if (redModel.coupon_price.floatValue <= 0.5) {
                redModel.backImage = imageList[3];
            }else if (redModel.coupon_price.floatValue > 0.5 && redModel.coupon_price.floatValue <= 1.0) {
                redModel.backImage = imageList[2];
            }else if (redModel.coupon_price.floatValue <= 1.5 && redModel.coupon_price.floatValue > 1) {
                redModel.backImage = imageList[1];
            }else if (redModel.coupon_price.floatValue > 1.5) {
                redModel.backImage = imageList[0];
            }
        }
        [allHongBao addObject:redModel];
    }
    self.combineArr = [NSMutableArray array];//因为allHongbao内容会变化,所以在变化的时候直接重写查找可以合并的红包
    canCombineCnt =0;
    for (int i=0; i<allHongBao.count; i++) {
        //这里应该根据模型里面的内容判断现在模拟
        if (i%2) {
            ExpressRedPageModel * model = allHongBao[i];
            model.isCombined = YES;
            [self.combineArr addObject:allHongBao[i]];
            canCombineCnt ++;
        }
    }
    self.combineItem.enabled =canCombineCnt>0;
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (showMode==0) {
        
        ExpressRedPageModel * model = allHongBao[indexPath.row];
        if ([model.is_used isEqualToString:@"2"]) {
            for (UIViewController * tmp in self.navigationController.viewControllers) {
                if ([tmp isKindOfClass:[ConfirmExpressViewController class]]) {
                    ConfirmExpressViewController * vc = (ConfirmExpressViewController *)tmp;
                    //这里进行传值
                    vc.hongBaoJZ = model.coupon_price;
                    vc.hongBaoID = model.coupon_id;
                    [self.navigationController popToViewController:vc animated:YES];
                }
            }
        }
    }
    else if(showMode==1)//在合并模式下单元格的点击事件。
    {
        
        ExpressRedPageModel * model = self.combineArr[indexPath.row];
        model.isCombineSelected=!model.isCombineSelected;

        if (model.isCombineSelected ==YES) {//被选中了
            [self.selectedArr addObject:self.combineArr[indexPath.row]];
        }
        else
        {
            [self.selectedArr removeObject:self.combineArr[indexPath.row]];
        }
        
        NSIndexPath *refreshCell = indexPath;
        [hongBaoTable reloadRowsAtIndexPaths:[NSArray arrayWithObjects:refreshCell, nil] withRowAnimation:UITableViewRowAnimationRight];
    }
   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    HongBaoTableViewCell *myHongBaoCell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (! myHongBaoCell) {
        myHongBaoCell = [[HongBaoTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    if (showMode==0) {//展示所有的
        
        if (allHongBao != nil && ![allHongBao isEqual:[NSNull null]] && [allHongBao count] > 0) {
            ExpressRedPageModel * model =allHongBao[indexPath.row];
            myHongBaoCell.redPageModel = model;
            model.isCombineSelected=NO;//展现所有的红包的时候不该被选择
            model.cellShowMode = showMode;
        }
    }
    else if(showMode ==1)//展示可以合并的
    {
        if (self.combineArr != nil && ![self.combineArr isEqual:[NSNull null]] && [self.combineArr count] > 0) {
            ExpressRedPageModel * model =self.combineArr[indexPath.row];
            model.cellShowMode = showMode;
            myHongBaoCell.redPageModel = model;
        }
    }
    return myHongBaoCell;
}

用模型的话可以添加些注释,而且模型对象通过点语法访问属性的时候可以看到自己注释的内容,减少了代码的来回粘贴、




内容概要:本文详细探讨了基于MATLAB/SIMULINK的多载波无线通信系统仿真及性能分析,重点研究了以OFDM为代表的多载波技术。文章首先介绍了OFDM的基本原理和系统组成,随后通过仿真平台分析了不同调制方式的抗干扰性能、信道估计算法对系统性能的影响以及同步技术的实现与分析。文中提供了详细的MATLAB代码实现,涵盖OFDM系统的基本仿真、信道估计算法比较、同步算法实现和不同调制方式的性能比较。此外,还讨论了信道特征、OFDM关键技术、信道估计、同步技术和系统级仿真架构,并提出了未来的改进方向,如深度学习增强、混合波形设计和硬件加速方案。; 适合人群:具备无线通信基础知识,尤其是对OFDM技术有一定了解的研究人员和技术人员;从事无线通信系统设计与开发的工程师;高校通信工程专业的高年级本科生和研究生。; 使用场景及目标:①理解OFDM系统的工作原理及其在多径信道环境下的性能表现;②掌握MATLAB/SIMULINK在无线通信系统仿真中的应用;③评估不同调制方式、信道估计算法和同步算法的优劣;④为实际OFDM系统的设计和优化提供理论依据和技术支持。; 其他说明:本文不仅提供了详细的理论分析,还附带了大量的MATLAB代码示例,便于读者动手实践。建议读者在学习过程中结合代码进行调试和实验,以加深对OFDM技术的理解。此外,文中还涉及了一些最新的研究方向和技术趋势,如AI增强和毫米波通信,为读者提供了更广阔的视野。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值