这篇为通过button控制跳转到ViewControllerPUSH 一个QQ好友列表

来源:互联网 发布:如何写js脚本抢票 编辑:程序博客网 时间:2024/06/05 18:49

//

//  ViewController2.h

//  Tab bar controllre

//

//  Created by 红珊瑚 on 15/5/25.

//  Copyright (c) 2015 红珊瑚. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface ViewController2 :UIViewController


@end






//

//  ViewController2.m

//  Tab bar controllre

//

//  Created by 红珊瑚 on 15/5/25.

//  Copyright (c) 2015 红珊瑚. All rights reserved.

//


#import "ViewController2.h"

#import "ViewControllerPUSH.h"


@interface ViewController2 ()


@end


@implementation ViewController2


- (void)viewDidLoad {

    [superviewDidLoad];

    self.view.backgroundColor = [UIColororangeColor];

    self.navigationItem.title =@"首页2";

    

    //创建按钮

   UIButton *button = [[UIButtonalloc] initWithFrame:CGRectMake(60,150, 80, 60)];

    button.backgroundColor = [UIColorpurpleColor];

    [button setTitle:@"push"forState:UIControlStateNormal];

    [self.viewaddSubview:button];

    //按钮的操作

    [button addTarget:selfaction:@selector(pushToViewController:)forControlEvents:UIControlEventTouchUpInside];

}


-(void)pushToViewController:(UIButton*)button

{

    //1.初始化名人的子视图控制器

    ViewControllerPUSH *push1 = [[ViewControllerPUSHalloc] init];

     //push的时候隐藏底部的 bar

    self.hidesBottomBarWhenPushed =YES;

    

    //2.push1视图控制器添加到导航视图控制器

    [self.navigationControllerpushViewController:push1 animated:YES];

     //push的时候隐藏底部的 bar

    self.hidesBottomBarWhenPushed =NO;

}




@end





//

//  ViewControllerPUSH.h

//  Tab bar controllre

//

//  Created by 红珊瑚 on 15/5/26.

//  Copyright (c) 2015 红珊瑚. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface ViewControllerPUSH :UIViewController


@end





//

//  ViewControllerPUSH.m

//  Tab bar controllre

//

//  Created by 红珊瑚 on 15/5/26.

//  Copyright (c) 2015 红珊瑚. All rights reserved.

//


#import "ViewControllerPUSH.h"


@interface ViewControllerPUSH ()<UITableViewDataSource,UITableViewDelegate>

{

   NSArray *sectionArr;

   NSArray *rowArr;

   UITableView *TaVi;

    NSMutableDictionary *showDic;

}


@end


@implementation ViewControllerPUSH


- (void)viewDidLoad {

    [superviewDidLoad];

    

    self.view.backgroundColor = [UIColorcolorWithRed:200/255.0

                                               green:100/255.0

                                                blue:143/255.0

                                               alpha:1.0f];

    self.navigationItem.title =@"QQ好友列表";

    

    //初始化tableview

    TaVi = [[UITableViewalloc] initWithFrame:self.view.bounds

                                       style:UITableViewStyleGrouped];

    TaVi.backgroundColor = [UIColorlightGrayColor];

   TaVi.delegate =self;

    TaVi.dataSource =self;

    [self.viewaddSubview:TaVi];

    

    //初始化sectionArr

    sectionArr = [NSArrayarrayWithObjects:@"好友",@"家人",@"同学",@"陌生人",@"黑名单",nil];

    rowArr = [NSArrayarrayWithObjects:@"张三",@"李四",@"王五",@"朱六",@"未命名",@"未命名",nil];

    

}


//添加代理方法

  //1. 分组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return [sectionArrcount];

}


  //2. cell行数

- (NSInteger)tableView:(UITableView *)tableView

 numberOfRowsInSection:(NSInteger)section

{

   return [rowArrcount];

}


 //3. cell行的高度

- (CGFloat)tableView:(UITableView *)tableView

heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    if ([showDicobjectForKey:[NSStringstringWithFormat:@"%ld",indexPath.section]])

    {

       return 60;

    }

   return 0;

}


 //4. section头部高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

   return 50;

}


 //5. 显示每个cell内容

- (UITableViewCell *)tableView:(UITableView *)tableView

         cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefault

                                     reuseIdentifier:nil];

//  cell.selectionStyle = UITableViewCellStyleDefault;

    

    //插图分离器

    cell.separatorInset =UIEdgeInsetsZero;//边缘插入图为零

    

    //剪辑到边界 *

    cell.clipsToBounds =YES;

    

    //cell添加第几行,和添加每行内容

    cell.textLabel.text = [NSStringstringWithFormat:@" %ld: %@",indexPath.row +1,rowArr[indexPath.row]];

    

   return cell;

}


  //6. section头部显示的内容

- (UIView *)tableView:(UITableView *)tableView

viewForHeaderInSection:(NSInteger)section

{

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

    headerView.backgroundColor = [UIColorpurpleColor];

    

    //UIView上创建一个UILabel用来显示分组名称

   UILabel *Label = [[UILabelalloc] initWithFrame:CGRectMake(10,15, 300, 20)];

    Label.textColor = [UIColororangeColor];

    Label.text = [NSStringstringWithFormat:@"分组- %ld 名称:%@",section +1,sectionArr[section]];

    [headerViewaddSubview:Label];

    

    //初始化手势识,单击的recognizer,收缩分组cell

    UITapGestureRecognizer *singleRecognizer = [[UITapGestureRecognizeralloc] initWithTarget:self

                                                                                      action:@selector(singleTap:)];

      //点击的次数=1

    singleRecognizer.numberOfTapsRequired =1;

      //1个手指操作

    [singleRecognizer setNumberOfTouchesRequired:1];

    

      //添加一个手势监测,到视图

    [headerViewaddGestureRecognizer:singleRecognizer];

    

    headerView.tag = section;


   return headerView;

}



  //7.实现手势监听事件的方法

- (void)singleTap:(UITapGestureRecognizer *)recognizer

{

    //获取点击的分组下标 tag

   NSInteger didSection = recognizer.view.tag;

    NSString *key = [NSStringstringWithFormat:@"%ld",didSection];

    

    //创建字典,判断该分组字典内键key=tag的元素是否为“A”,如果是则展开,否则 收缩隐藏

   if (!showDic)

    {

        showDic = [[NSMutableDictionaryalloc] init];

    }

    

   if (![showDicobjectForKey:key])

    {

        [showDicsetObject:@"A"forKey:key];

    }

   else

    {

        [showDicremoveObjectForKey:key];

    }

    

    //刷新当前tag所在的分组,实现展开收缩功能

    [TaVireloadSections:[NSIndexSetindexSetWithIndex:didSection]

        withRowAnimation:UITableViewRowAnimationFade];

    

}




@end



0 0
原创粉丝点击