Leif160519的blog Leif160519的blog

——————

目录
UINavigation导航栏和UITableviewcell点击事件写法
/    

UINavigation导航栏和UITableviewcell点击事件写法

UINavigation和UItableViewCell是OC项目开发中必定会用到的东西,其中有很多常用的功能和写法,在此我简单的整理一下

先看一下示例程序
程序演示
viewcontroller图层布局
secondViewcontroller图层布局

其中涉及到的用法有:

  • UINavigation导航栏的创建
  • 带导航栏页面的跳转
  • CELL的重用
  • *UItableviewCell点击事件
  • 背景颜色的更改
  • 导航栏标题的更改
  • 页面之间属性传值的用法
  • Tag值
  • Tabbar在跳转之后的消失

创建项目什么的我就不再赘述了,下面直接贴代码

先创建一个ViewController类型的类。名字叫SecondViewController

SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

//声明一个nsstring类型的属性,便于之后的属性传值
@property (nonatomic,strong)NSString * type;
@end

SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

//点击跳转过后,tabbar消失,点击返回后出现
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibNameOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if ([_type isEqualToString:@"101"]) {
        self.view.backgroundColor = [UIColor whiteColor];
    }else if ([_type isEqualToString:@"102"])
    {
        self.view.backgroundColor = [UIColor redColor];
        
    }else if ([_type isEqualToString:@"103"])
    {
        self.view.backgroundColor = [UIColor yellowColor];
        
    }else if ([_type isEqualToString:@"104"])
    {
        self.view.backgroundColor = [UIColor greenColor];
        
    }else if ([_type isEqualToString:@"105"])
    {
        self.view.backgroundColor = [UIColor blueColor];
    }
    else
    {
        self.view.backgroundColor = [UIColor grayColor];
    }
    
}

@end

APPDelegate.m中在第一个方法中添加如下代码(添加首页头文件):

	//设置跟视图的背景色为白色(此举是为了避免页面跳转的时候出现页面显示问题,待会放图)
	self.window.backgroundColor = [UIColor whiteColor];
	
	//初始化首页
    ViewController * VC = [[ViewController alloc]init];
    
    //新建一个navigationcontroller并初始化

    //新建底部菜单栏
    UITabBarController * tabBar = [[UITabBarController alloc]init];
    
    //把首页导航栏添加到tabbar中,如果有第二个第三个页面,也可以依次在[]中添加,并且以“,”隔开
    tabBar.viewControllers = @[nav];
    
    //把tabbar设置为跟视图
    self.window.rootViewController = tabBar;

**注意:**上面提到,如果不设置window的背景色,会出现页面跳转的显示问题,大家看一下区别

这里写图片描述

有没有发现?导航栏右边会有一个黑色模糊的区域,这是非常难看的

ViewController.m中(UITableview我没有采用xib拖动的方式,我直接用的代码,为的就是一次性看清楚)

#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView * mainTableView;

@end

@implementation ViewController
-(id)init
{
    if (self = [super init]) {
        self.title = @"首页";
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _mainTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
//去除多余的cell分割线
    _mainTableView.tableFooterView = [[UIView alloc]init];
//    _mainTableView.backgroundColor = [UIColor yellowColor];
//    self.view.backgroundColor = [UIColor redColor];
    [self.view addSubview:_mainTableView];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else if (section == 1)
    {
        return 2;
    }
    else
    {
        return 3;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * ID = @"My cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        
    }
    
//    cell.backgroundColor = [UIColor grayColor];
    cell.textLabel.text = [NSString stringWithFormat:@"第%ld组,第%ld行",indexPath.section, (long)indexPath.row];

//设置cell右侧的指示箭头,表示点击还有页面跳转
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//设置点击cell的时候能立即恢复原来的状态而不是点击一下立即变成灰色
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//属性传值,必须先初始化,值传递过去之后就不能再次初始化了,因为这个传递过程中是带参数的,再次初始化过后就没了
    SecondViewController * secVC = [[SecondViewController alloc]init];
    secVC.title = [NSString stringWithFormat:@"第%ld组,第%ld行",indexPath.section,indexPath.row];

//带有导航栏的跳转方式,没有导航栏的不能这么写
    [self.navigationController pushViewController:secVC animated:YES];
    if (indexPath.section == 0) {
        secVC.type = @"101";
    }else if (indexPath.section == 1)
    {
        if (indexPath.row == 0) {
            secVC.type = @"102";
        }else
        {
            secVC.type = @"103";
        }
    }
    else
    {
        if (indexPath.row == 0) {
            secVC.type = @"104";
        }
        else if (indexPath.row == 1)
        {
            secVC.type = @"105";
        }
        else
        {
            secVC.type = @"106";
        }
    }
}
}


@end

在ViewController中我注解掉的代码,有兴趣的朋友可以取消注解运行一下看看是什么效果。



“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill

标  题UINavigation导航栏和UITableviewcell点击事件写法
作  者Leif160519
出  处https://github.icu/articles/2019/08/23/1566542772948.html
关于博主:坐标南京,运维工程师,如有问题探讨可以直接下方留言。
声援博主:如果您觉得文章对您有帮助,可以评论、订阅、收藏。您的鼓励是博主的最大动力!