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

上篇文章说了自己在公司代码遇到的问题,可维护性很低,做了很多无用功才熟悉代码。点击打开链接http://blog.csdn.net/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:@"[email protected]",@"[email protected]",@"[email protected]",@"[email protected]",@"[email protected]", 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;
}

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




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章