UITableView-------模型的使用

来源:互联网 发布:windows 10 x64 th2 编辑:程序博客网 时间:2024/05/22 20:29

余额查询示例

1. AccountCheckViewController.h

#import <UIKit/UIKit.h>

#import "NetPhoneServerController.h" -------------封装的请求类

@interface AccountCheckViewController :UIViewController<NetPhoneServerControllerDelegate,UITableViewDelegate,UITableViewDataSource>

{

   NetPhoneServerController       *_netPhoneServerController;

}

@property (retain,nonatomic)IBOutletUITableView *comboTableView; 

2. AccountCheckViewController.m

-(void)viewWillAppear:(BOOL)animated

{

    [superviewWillAppear:animated];

    //cheackRequest 查询请求

    [selfcheackRequest];

}

- (void)viewDidLoad {

    [superviewDidLoad];

    self.navigationController.navigationBarHidden =NO;

   self.title =@"余额查询";

   UIView *footView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,320, 60)];

   NSArray *titleArr =@[@"去充值",@"查询话单"];

   for (int i =0; i < titleArr.count; i ++) {

       UIButton *btn = [[UIButtonalloc]initWithFrame:CGRectMake(30 + (80100) * i,30,80, 30)];

        

        btn.tag = i;

        btn.layer.masksToBounds =YES;

        [btn.layersetBorderColor:[UIColorcolorWithRed:9.0/255.0green:148.0/255.0blue:252.0/255.0alpha:0.8].CGColor ];//边框颜色

        [btn setTitle:titleArr[i]forState:UIControlStateNormal];

        [btn setTitleColor:[UIColorblueColor]forState:UIControlStateNormal];

        [btn addTarget:selfaction:@selector(clickAction:)forControlEvents:UIControlEventTouchUpInside];

        [footViewaddSubview:btn];

    }

   self.comboTableView.tableFooterView = footView;

   UIView *headerView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,320, 50)];

   UILabel *phoneLabel =[[UILabelalloc]initWithFrame:CGRectMake(20,0,300, 50)];

    phoneLabel.textColor = [UIColororangeColor];

    phoneLabel.font = [UIFontboldSystemFontOfSize:18];

    phoneLabel.text = [NSStringstringWithFormat:@"当前账号:%@",[CommonFuncGetLocalDataString:DATA_STORE_MOBILE]]; --------------取出登陆存储在本地的手机号码

    [headerViewaddSubview:phoneLabel];

   self.comboTableView.tableHeaderView = headerView;

}

- (void)cheackRequest

{

    NSMutableDictionary *dic = [NSMutableDictionarydictionaryWithCapacity:0];

    _netPhoneServerController = [[NetPhoneServerControlleralloc]init];

    [_netPhoneServerControllersetDelegate:self];

    [_netPhoneServerControllerrequestHttpServerType:getRequestTyperequestNetPhoneServerType:SearchBalanceTypeInfoDic:dic :BalancePath];

    

    [SVProgressHUDshowInView:self.view

                      status:@"查询中..."

             networkIndicator:YES

                        posY:-1

                     maskType:SVProgressHUDMaskTypeClear];

}

- (void)requestNetPhoneServerFinish: (NSDictionary *)dic returnString: (NSString *)str serverType: (RequestNetPhoneServerType)netPhoneServerType

{

    

   NSLog(@"%@",dic);

    [UIApplicationsharedApplication].networkActivityIndicatorVisible =NO;

    

   int nRet = [[dicobjectForKey:@"status"]intValue];

   BOOL bSuccess =NO;

    

   switch (nRet)

    {

       case1:

        {

           model = [AccountCheckModelshareInstance];

           model.accountType = [modelisNoDayOrTimef:dic];

            if (model.accountType == AccountChectTypeDayAndTime) {

               count =2;

            }else{

               count =1;

            }

            [self.comboTableViewreloadData];

            bSuccess =YES;

        }

           break;

    }

   NSString *strMsg = dic[@"msg"];

   if (nil == strMsg) {

        strMsg =@"";

    }

   if (bSuccess) {

        [SVProgressHUDdismissWithSuccess:strMsgafterDelay:1];

    }else {

        [SVProgressHUDdismissWithError:strMsgafterDelay:1];

    }

}

#pragma mark -----------UITableViewDelegate/DataSource-------------------


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

   if (section ==0) {

       UIView *accountView = [[UIViewalloc]initWithFrame:CGRectMake(20,22,300, 20)];

       UILabel *accountLabel = [[UILabelalloc]initWithFrame:CGRectMake(0,0,300, 20)];

        accountLabel.text = [NSStringstringWithFormat:@"当前账号: %@",[CommonFuncGetLocalDataString:DATA_STORE_MOBILE]];

        accountLabel.textColor = [UIColororangeColor];

        [accountViewaddSubview:accountLabel];

       return accountView;

    }

    return nil;

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

   returncount;

}

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

{

   return1;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

   if (count ==2) {

       if (indexPath.section ==1) {

           return65;

        }

    }

   return173;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

   staticNSString *identifier =@"Cell";

   UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:identifier];

   if (nil == cell) {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];

    }

   AccountDayView *dayView = [[AccountDayViewalloc]initWithFrame:CGRectMake(0,0,320, 150)];

    

    if (model.accountType == AccountChectTypeDayAndTime) {

       switch (indexPath.section) {

           case0:

            {

                

                [dayView setComboLabel:@"当前套餐:包月"DayCount:model.monthDayValidity:@""WithDay:@""];

                [celladdSubview:dayView];

            }

               break;

           case1:

            {

               AccountTimeView *timeView = [[AccountTimeViewalloc]initWithFrame:CGRectMake(0,0,320, 60)];

               NSString *money = [NSStringstringWithFormat:@"余额:         %@",model.money];

               NSString *vilibityStr = [NSStringstringWithFormat:@"有效期至:  %@",model.validate];

                [timeViewsetMoney:moneyWithVilibity:vilibityStr];

                [celladdSubview:timeView];

            }

               break;

                

           default:

               break;

        }

    }else{

        if(model.accountType == AccountChectTypeDay){

            [dayView setComboLabel:@"当前套餐:包月"DayCount:model.monthDayValidity:@""WithDay:@""];

            

        }elseif (model.accountType ==AccountChectTypeTime){

            dayView.noUseComboLabel.text =@"";

           NSString *validityStr = [NSStringstringWithFormat:@"有效期至  %@",model.validate];

            [dayViewsetComboLabel:@"当前套餐:分钟"DayCount:model.moneyValidity:validityStrWithDay:@""];

        }else{

            [dayView setComboLabel:@"当前套餐:包月"DayCount:@"0"Validity:@""WithDay:@""];

        }

        dayView.noUseComboLabel.text =@"";

        [celladdSubview:dayView];

    }

   return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    [tableView deselectRowAtIndexPath:indexPathanimated:NO];

}


-------------------------------华丽的分割线--------------------------------------------

3.AccountCheckModel.h ------模型

#import <Foundation/Foundation.h>


typedef enum{

    AccountChectTypeDay              =1000,   --包天卡

    AccountChectTypeTime             =1001,   --计时卡

    AccountChectTypeDayAndTime       =1002,   --2种套餐

    AccountChectTypeNone             =1003    --未购买套餐

}ACCOUNTCHECKTYPE;

@interface AccountCheckModel :NSObject

@property (nonatomic,strong)NSString *money;

@property (nonatomic,strong)NSString *monthDay;

@property (nonatomic,assign)int type;

@property (nonatomic,strong)NSString *validate;

@property (nonatomic)ACCOUNTCHECKTYPE accountType;

+ (AccountCheckModel *)shareInstance;

- (ACCOUNTCHECKTYPE)isNoDayOrTimef:(NSDictionary *)accountDic;-----判断套餐的方法


4.AccountCheckModel.m

+ (AccountCheckModel *)shareInstance

{

    staticAccountCheckModel *model =nil;

   if (nil == model) {

        model = [[AccountCheckModelalloc]init];

    }

   return model;

}

- (ACCOUNTCHECKTYPE)isNoDayOrTimef:(NSDictionary *)accountDic

{

    

   self.money = accountDic[@"money"];

   self.monthDay = accountDic[@"monthday"];

   self.type = [accountDic[@"type"]intValue];

   self.validate = accountDic[@"validate"];

    //包天计时

   if ([self.moneyfloatValue] !=0 && [self.monthDayfloatValue] !=0) {

        returnAccountChectTypeDayAndTime;

    }elseif ([self.monthDayfloatValue]!=0 && [self.moneyfloatValue] ==0){

        returnAccountChectTypeDay;

    }elseif ([self.monthDayfloatValue] == 0 && [self.moneyfloatValue] !=0){

        returnAccountChectTypeTime;

    }

   self.monthDay =0;

    returnAccountChectTypeNone;

}


5.AccountDayView   包天套餐视图

-(instancetype)initWithFrame:(CGRect)frame

{

   self = [superinitWithFrame:frame];

   if (self) {

       self.backgroundColor = [UIColorwhiteColor];

       //当前套餐类型

        comboLabel = [[UILabelalloc]initWithFrame:CGRectMake(20,20,300, 20)];

        comboLabel.font = [UIFontboldSystemFontOfSize:18];

       comboLabel.text =@"当前套餐:包月";

       //剩余

        surplusLabel = [[UILabelalloc]initWithFrame:CGRectMake(20,100,60, 20)];

       surplusLabel.text =@"剩余";

        surplusLabel.font = [UIFontboldSystemFontOfSize:16];

       //天数

        dayLabel = [[UILabelalloc]initWithFrame:CGRectMake(80,68,185, 60)];

       dayLabel.textColor = [UIColororangeColor];

        dayLabel.textAlignment =NSTextAlignmentRight;

        dayLabel.font = [UIFontboldSystemFontOfSize:60.0];

       dayLabel.text =@"10000";

       //

        dayLab = [[UILabelalloc]initWithFrame:CGRectMake(280,100,20, 20)];

       dayLab.text =@"";

        dayLab.font = [UIFontboldSystemFontOfSize:17.0];

       //有效期

        self.validityLabel = [[UILabelalloc]initWithFrame:CGRectMake(20,128,300, 20)];

       self.validityLabel.text =@"有效期";

        self.validityLabel.font = [UIFont boldSystemFontOfSize:17];

       //未使用套餐

        self.noUseComboLabel = [[UILabelalloc]initWithFrame:CGRectMake(20,153,260, 20)];

        self.noUseComboLabel.text =@"未使用套餐";

        self.noUseComboLabel.font = [UIFont boldSystemFontOfSize:16];

        

        

        [selfaddSubview:comboLabel];

        [selfaddSubview:surplusLabel];

        [selfaddSubview:dayLab];

        [selfaddSubview:dayLabel];

        [selfaddSubview:self.validityLabel];

        [selfaddSubview:self.noUseComboLabel];

        

    }

    return self;

}


- (void)setComboLabel:(NSString *)comboStr DayCount:(NSString *)dayStr Validity:(NSString *)validityStr WithDay:(NSString *)day

{

   comboLabel.text = comboStr;

   dayLabel.text = dayStr;

   self.validityLabel.text = validityStr;

   dayLab.text = day;

}



6.AccountTimeView  计时套餐视图

-(instancetype)initWithFrame:(CGRect)frame

{

   self = [superinitWithFrame:frame];

   if (self) {

        

       UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(20,0,320, 20)];

        label.text =@"当前套餐:分钟";

        label.textColor = [UIColororangeColor];

        label.font = [UIFontboldSystemFontOfSize:15];

        

        moneyLabel = [[UILabelalloc]initWithFrame:CGRectMake(20,22,320, 20)];

        moneyLabel.font = [UIFontboldSystemFontOfSize:15];

       moneyLabel.text =@"余额";

        

        vilibityLabel = [[UILabelalloc]initWithFrame:CGRectMake(20,43,320, 20)];

       vilibityLabel.text =@"有效期至";

        vilibityLabel.font = [UIFontboldSystemFontOfSize:15];

        

        [selfaddSubview:label];

        [selfaddSubview:moneyLabel];

        [selfaddSubview:vilibityLabel];

    }

    return self;

}

- (void)setMoney:(NSString *)moneyStr WithVilibity:(NSString *)vilibityStr

{

   moneyLabel.text = moneyStr;

   vilibityLabel.text = vilibityStr;

}




0 0
原创粉丝点击