上篇文章說了自己在公司代碼遇到的問題,可維護性很低,做了很多無用功才熟悉代碼。點擊打開鏈接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;
}
用模型的話可以添加些註釋,而且模型對象通過點語法訪問屬性的時候可以看到自己註釋的內容,減少了代碼的來回粘貼、