iOS ——点击sectionHeader进行跳转,不同的sectionHeader标签不同

  最近被要求第几sectionHeader进行跳转界面,不同的sectionHeader有不一样的标题,图如下图所示:

      

         上海世博那一块就是sectionHeader点击进行跳转,并且有两个section每个section的内容都不同,代码如下:

         里边添加了手势:UITapGestureRecognizer,很多博客里写道了单手指或多手指的手势,这里就不讲了,因为妹子我也没有学会,基本就是要求做什么,学什么。

       如果你仅仅是想在不同的section中添加不同的开头,我在另一个博客中已经写过了,并且有我不想总是转载别人的,网上一查都是一样的代码还是不一样的人,很是心累的呀小伙伴们!

         好的,废话美元了,代码如下:

       1.首先确保你有tableView,并且会建立cell,份section,不会的话再学学~

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

       {

    return 2;           //section的数目2个

         }

         2.主要代码,别的不需要,当然前提是你真的会建立cell,只要在你简历的文件中打入这段话就可以了,具体跳转的方法也是要说明一下的

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

  {

    UIView *view = [[UIView alloc] init];//我在sectionHeader中建立了一个View

    if (section ==0){               //判断为哪个section

    UITapGestureRecognizer *tapg = [[UITapGestureRecognizer alloc]init];

    [tapg addTarget:self action:@selector(sj_tap:)];      //跳转的动作,妹子是要用sj_tap:定义我的跳转函数

    tapg.numberOfTapsRequired=1;                  //一个手指的跳转,大家知道苹果可以创建多手指手势

    [view setUserInteractionEnabled:YES];

    UILabel *label =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];  //必须设置CGRect不然界面上一开始创建了找不到,妹子开始就这样

    [label setText:@"上海世博有限公司"];          //添加我的字,这个label和平时label的设置方法一样

    [view addGestureRecognizer:tapg];

    [view addSubview:label];

    }

    if (section == 1){       //这个和上一个一样,我没写全

    UITapGestureRecognizer *tapg1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sj_tap:)];

    UILabel *label1 =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 220, 30)];

    [label1 setText:@"上海世博供应链有限公司"];

    [view addGestureRecognizer:tapg1];

    [view addSubview:label1];

    }

    return view;

}

我的跳转代码,这个我觉得应该写出来,因为细节可能让你调很久,妹子只是自己的经验之谈

- (void)sj_tap:(UIGestureRecognizer*)sender //注意与平时的不同

{

    TEWEditExpoSubsidiarySupervisorViewController *Te = [[TEWEditExpoSubsidiarySupervisorViewController alloc]init]; //妹子计较要跳转到的屏幕的名字

   [self presentViewController:Te animated:YES completion:nil];

}

只要把这两段代码加进去就可以了,没有多余的设置,.h文件就是你原本的,要的就是直接可以用的代码。希望能对初学者有用。

    我的giuHub地址:https://github.com/leroypus/WGJDemo.git,上边会传一些我做的纯代码做界面的demo和我再做的一些项目demo,欢迎follow,欢迎点星。这个项目在完成以后也会上传,需要的朋友可以过段时间去下载。

原文地址:https://www.cnblogs.com/lepus/p/4830098.html