ios 修改标签栏的第4个子页面 (4)


接下来,我们要修改的是tabBar容器下的第4个子页面,界面如下。


点击“我的收藏”、“我的订单”、“个人设置”、“关于”可以跳转至相应的下一界面。具体说来,比如,点击“我的收藏”时,跳转的下一页面的标题会是“我的收藏”。点击“退出登录”会跳出确认对话,点击其中的“退出登录”,会跳转至初始登录页面。


首先,对第4个子界面进行界面布局,用到了UITableView组件的分组样式,即UITableViewStyleGrouped。一共有三个分组,第一分组有一行,第二分组有四行,第三分组有一行,

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ //一共有多少个分组
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ //每个分组有几行
    if(section==0){
        return 1;
    }else if (section==1){
        return 4;
    }else {
        return 1;
    }
}

可以通过以下语句给各分组设定不同的高度。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row==0&&indexPath.section==0){
        //row==0表示第1行,section==0表示第1个分组,与操作,设置第一个分组第一行的高度为80,其余为50
        return 80;
    }else{
        return 50;
    }
}

通过@property NSMutableArray *arraysection语句声明一个数组 arraysection ,然后在- (void)viewDidLoad { }中为数组赋值,这样使得数据便于管理,且更加简洁直观。

_arraysection=[[NSMutableArray alloc]initWithObjects:@"我的收藏",@"我的订单",@"个人设置",@"关于", nil];

然后,为表格填充内容。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier=@"identifier";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];//cell的重用机制
    }
    cell.backgroundColor=[UIColor whiteColor];
    //依次传值
    if (indexPath.section==0) {
        cell.imageView.image=[UIImage imageNamed:@"i.jpeg"];
        cell.textLabel.text=@"AD";
    }else if (indexPath.section==1){
//        if(indexPath.row==0){
//            cell.textLabel.text=@"我的收藏";
//        }else if (indexPath.row==1){
//            cell.textLabel.text=@"我的订单";
//        }else if (indexPath.row==2){
//            cell.textLabel.text=@"个人设置";
//        }else{
//            cell.textLabel.text=@"关于";
//        }
//         //依次给各cell赋值
        cell.textLabel.text=[_arraysection objectAtIndex:indexPath.row];//通过数组传递
    }else{
        cell.textLabel.text=@"退出登录";
        cell.textLabel.textAlignment=NSTextAlignmentCenter; //设置内容居中
    }
    return cell;
}

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

方法里面添加触发cell的响应事件。

确认对话用到的控件是Action Sheets(功能表单)

点击“我的收藏”等按键,跳转至下一界面,只需要声明一个新的navigationController容器,即创建新的SectionViewController类,改变title属性值就行。

至于下一界面的标题与各按键相对应则用到了界面传值中的属性传值方法  

 [ 属性传值可以用于A→B(A跳转到B,即顺向传值)]

于是,下一界面即SectionViewController.m中只需用到 self.title = self.titleString;  即可传递标题值。
代码如下

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //触发cell响应事件
    if(indexPath.section==2){
              //Action Sheet功能表单
        UIAlertController *action=[UIAlertController alertControllerWithTitle:nil message:@"确认退出?" preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *exit=[UIAlertAction actionWithTitle:@"退出登录" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            [self jumpToLogin];  //点击退出登录后,跳转至jumpToLogin方法
        }];
        UIAlertAction *cancel=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [action addAction:exit];  //为该Action Sheet添加exit按键
        [action addAction:cancel];  //为该Action Sheet添加cancel按键
        [self presentViewController:action animated:YES completion:nil]; //在界面上显示
        }else if (indexPath.section==1){
            //依次传值
//        if(indexPath.row==0){
//            SectionViewController *sectionVC = [[SectionViewController alloc] init];
//            sectionVC.titleString = @"我的收藏";  //属性传值方法,在头文件中声明了titleString是字符串型
//            sectionVC.hidesBottomBarWhenPushed = YES; //隐藏底部的标签栏
//            [self.navigationController pushViewController:sectionVC animated:YES];
//        }else if (indexPath.row==1){
            
//        }else if (indexPath.row==2){
            
//        }else{
            
//        }
        SectionViewController *sectionVC = [[SectionViewController alloc] init]; //初始化下一界面
        sectionVC.titleString =[_arraysection objectAtIndex:indexPath.row];//属性传值,通过数组传递
        sectionVC.hidesBottomBarWhenPushed = YES; //在下一界面隐藏底部的标签栏
        [self.navigationController pushViewController:sectionVC animated:YES];//跳转至下一界面
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; //取消cell的选中状态
}

值得注意的是选中cell之后,会有选中状态,如下所示。

所以需要通过[ tableView deselectRowAtIndexPath:indexPath animated:YES]语句,取消表格中各行的选中状态。

jumpToLogin方法声明如下。

-(void)jumpToLogin{
    AppDelegate *delegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate changeToLogin];
}

其中这个 changeToLogin方法在AppDelegate中声明,

- (void)changeToLogin {  //自定义的方法.注意要在.h文件中声明
    ViewController *login=[[ViewController alloc]init];
    self.window.rootViewController=login;
}

至此,基本完成。

猜你喜欢

转载自blog.csdn.net/always_kyathe/article/details/81050534