为了能使UITableView作为根视图能够兑现页面跳转,垃圾回收中央

再续前缘


此番还是对UITableView就是修补。这一次给她加上2个UINavigationController,这么些有啥用?字面意思啊!导航啊!
当您须求跳转到下3个页面包车型大巴时候,用UINavigationController将下2个页面放到栈顶。恩,便是那样用的。
<br />

再接再砺


上面将在初阶敲代码啦!
先建三个分组,恩,为了赏心悦目。
<br />

又要对AppDelegate动手啦


为了能使UITableView作为根视图可以实现页面跳转,大家就要对AppDelegate入手。将根视图调控器由RootViewController改动为UINavigationController,再将RootViewController作为UINavigationController的根视图。
大概涉及正是那般:
UIApplication — UIWindow — UINavigation — RootView — UITableView —
UITableViewCell

RootViewController


图片 1

选中NEW GROUP
自身给它定名叫GarbageCenter,后边的全局类作者都会扔在这边,垃圾回收中央!
按下Command+N,创设一个新对象Cocoa touch
class。并给它贰个名字:RootViewController,并让其承袭UIViewController。

图片 2

Paste_Image.png

不打算用XIB开采,所以就把钩去了就好了!
Language默认Objective-C就好了。

图片 3

Paste_Image.png

恭贺你,第四个类那样就创办完毕了!
此时按下Command+CR-V,项目就可以跑起来了。可是到如今截止,项目仍旧会以StoryBoard的情势展开,那刚才新建的类要怎么用吗?不用急,慢慢来,耐心。
<br />

AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor grayColor];

    RootViewController *instance_rootView = [[RootViewController alloc]init];
    [instance_rootView.view setFrame:self.window.frame];

    UINavigationController *instance_navigation = [[UINavigationController alloc]initWithRootViewController:instance_rootView]; //初始化UINavigationController,并将RootViewController作为其根视图

    self.window.rootViewController = instance_navigation; //再将UINavigationController作为UIWindow的根视图
    [self.window makeKeyAndVisible];
    return YES;
}

后日Run一下,就能见到效果了:

效果

地点多了一条金色的章程,这个正是导航条了。导航条上边能够放什么?能够放开关、标题等等诸多。
唯独那些近乎不太雅观啊,大家做下全局样式的更动吗。
在此以前,笔者要先打包二个全局类,将有些相比常用,不过又麻烦的章程封装在里边。

代码格局Launch


咱俩再次归来AppDelegate里面。前面说过,在AppDelegate里面有个艺术:didFinishLaunchingWithOptions:,它是在应用软件运转完成后实行的办法。
而在APP运行之后,大家当然是亟需让应用程式的窗口体现到荧屏上,此时我们就要如此做:

#import "RootViewController.h"
//先导入刚才新建的类
...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //将窗口的大小设为和屏幕大小一样

    self.window.backgroundColor = [UIColor grayColor];
    //将窗口的背景颜色更改为灰色

    RootViewController *instance_rootView = [[RootViewController alloc]init];
    //初始化RootViewController

    [instance_rootView.view setFrame:self.window.frame];
    //设置RootViewController的大小和window的相同

    self.window.rootViewController = instance_rootView;
    //将RootViewController设置为根视图控制器

    [self.window makeKeyAndVisible];
    //然后设置窗口为可见

    return YES;
}

那是再Run项目事后,就不再是以StoryBoard的款式运维了。进入应用程式之后,看到的正是RootViewController的一片白茫茫。上面就到RootViewController了。

CommonHandler


测试菜单–列表


以此项指标目标是为了集成一大堆测试内容而建的,所以首页就相应做成二个菜谱。而在UIKit里面,就有三个很适合的类库,UITableView,它大约穿越了方方面面APP的开采周期,随地都到收获他。
我们先来探视例子。

#import "RootViewController.h"

//拓展了两个协议,UITableViewDataSource和UITableViewDelegate
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>

//初始化一个UITableView
@property (nonatomic, strong) UITableView *view_table;

@end

@implementation RootViewController

//页面加载的时候会执行这个方法
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self initRootView]; //调用自定义初始化RootView方法

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

//初始化RootView里面的内容
- (void)initRootView {
    self.view.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1]; //设置背景颜色

    self.view_table = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; //初始化UITableView,并设置起大小和RootView的大小一样,同时将UITableVIew的样式改为分组样式
    self.view_table.delegate = self; 
    self.view_table.dataSource = self;
    //设置UITableView的代理和数据源为RootView    

    [self.view addSubview:self.view_table]; //将UITableView添加到RootView上显示
}

//该方法适用于UITableViewStyleGrouped,返回分组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//该方法会返回每个组数里面包含的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

//返回每行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}

//返回每行的对象
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell_temp = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"cell%ld0%ld", indexPath.section, indexPath.row]];
    cell_temp.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell_temp;
}

@end

图片 4

运维结果

如此那般,一个唯有单行单组的表格视图就成功了!马到成功。
因为是个菜单嘛,只有一行怎么能行呢?先加上目录多少个字呢!

- (void)initTableHeaderView{
    UIView *view_header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 100)];

    UILabel *lb_menu = [[UILabel alloc]initWithFrame:view_header.frame];
    lb_menu.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];
    lb_menu.textAlignment = NSTextAlignmentCenter;
    lb_menu.font = [UIFont fontWithName:@"Arial" size:16.0];
    lb_menu.text = @"目 录";

    [view_header addSubview:lb_menu];

    self.view_table.tableHeaderView = view_header;
}

然后我们就会看到这些意义:

图片 5

运转结果

上边就是将这一个首页,稍微打包打包,让前边增加新的分组和行数据时亦可方便点。
变动后的代码:

#import "RootViewController.h"

@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *view_table;

@property (nonatomic, strong) NSArray *array_section;
@property (nonatomic, strong) NSArray *array_row;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self initRootView];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)initRootView {
    self.view.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];

    self.view_table = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.view_table.delegate = self;
    self.view_table.dataSource = self;

    [self.view addSubview:self.view_table];

    [self initTableHeaderView];
    [self initData];
}

- (void)initTableHeaderView{
    UIView *view_header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 100)];

    UILabel *lb_menu = [[UILabel alloc]initWithFrame:view_header.frame];
    lb_menu.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];
    lb_menu.textAlignment = NSTextAlignmentCenter;
    lb_menu.font = [UIFont fontWithName:@"Arial" size:16.0];
    lb_menu.text = @"目 录";

    [view_header addSubview:lb_menu];

    self.view_table.tableHeaderView = view_header;
}

- (void)initData{
    self.array_section = [NSArray arrayWithObjects:@"第一章", nil];

    self.array_row = [NSArray arrayWithObjects:
                      [NSArray arrayWithObjects:@"第一节", @"第二节", nil],
                      nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.array_section count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return [self.array_row[section] count];
            break;

        default:
            return 0;
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.array_section[section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell_temp = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"cell%ld0%ld", indexPath.section, indexPath.row]];
    cell_temp.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell_temp.selectionStyle = UITableViewCellSelectionStyleNone;

    cell_temp.textLabel.text = [self.array_row[indexPath.section] objectAtIndex:indexPath.row];

    return cell_temp;
}

@end

今后效果正是那样了:

图片 6

运作结果

<br />

CommonHandler.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface CommonHandler : NSObject

+(UIColor *)getColorWithRGB:(int)rgb;
+(UIColor *)getColorWithRed:(float)red andGreen:(float)green andBlue:(float)blue andAlpha:(float)alpha;

@end

最后


权且就到此处吧,下篇就到了轩然大波监听了。

CommonHandler.m
#import "CommonHandler.h"

#define RGB(r, g, b) [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:1.0]
#define RGBAlpha(r, g, b, a) [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:(a)]

#define HexRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define HexRGBAlpha(rgbValue,a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:(a)]

@interface CommonHandler()

@end

@implementation CommonHandler

+(UIColor *)getColorWithRGB:(int)rgb{
    return HexRGB(rgb);
}
+(UIColor *)getColorWithRed:(float)red andGreen:(float)green andBlue:(float)blue andAlpha:(float)alpha{
    return RGBAlpha(red, green, blue, alpha);
}

@end

<br />

对导航条动刀了


就那样,二个急忙获得颜色的公共类就解决了,比自带的更有利了。
反之亦然是对AppDelegate动手。

AppDelegate.m
[[UINavigationBar appearance] setBarTintColor:[CommonHandler getColorWithRGB:0xFFB6C1]];
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];

就增加这两句,就完了。

Paste_Image.png

<br />

导入AlertMessage


前面因为无聊,弄了个AlertMessage自定义类,今后得以移植到此地来做测试了!
因为早已打包好了,直接将八个公文复制进项目就可以一贯用了。在此间我放出代码:

AlertMessage.h
#import <UIKit/UIKit.h>

@interface AlertMessage : UIView

-(id)initWithY:(CGFloat)y andTitle:(NSString *)title;
-(id)initWithY:(CGFloat)y;
-(id)initWithTitle:(NSString *)title;
-(void)setStartWithY:(CGFloat)y;
-(void)setTitle:(NSString *)title;
-(void)showWithCover:(BOOL)boolean;
-(void)dismiss;
-(void)setTitleColor:(UIColor *)titleColor;
-(void)setAlertColor:(UIColor *)alertColor;
-(void)setAlertColor:(UIColor *)alertColor andTitleColor:(UIColor *)titleColor;
-(void)setDismissOnLeft:(BOOL)isOnLeft;
@end

然后就是达成文件

AlertMessage.m
#import "AlertMessage.h"

#define HEIGHT_ALERT 55
#define WIDTH_ALERT 180
#define Y_OFFSET_ALERT 100
#define X_OFFSET_ALERT (320 - WIDTH_ALERT)
#define HEIGHT_TITLE 25
#define GAP_DEFAULT 10
#define FONT_TITLE [UIFont systemFontOfSize:16]
#define RGB(r, g, b)    [UIColor colorWithRed:(r)/255.f green:(g)/255.f blue:(b)/255.f alpha:1.f]

@interface AlertMessage()

@property (nonatomic, strong) NSString *string_title;

@property (nonatomic, strong) UIColor *color_title;
@property (nonatomic, strong) UIColor *color_background;

@property (nonatomic, strong) UILabel *lb_title;

@property (nonatomic, strong) UIButton *btn_cover;

@property (nonatomic, assign) BOOL isOnLeft;

@property (nonatomic, assign) CGFloat y;

@end

@implementation AlertMessage

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id)initWithY:(CGFloat)y andTitle:(NSString *)title{
    self = [self initAlert];
    if(self){
        [self setStartWithY:y];
        [self initContentWithTitle:title];
    }
    return self;
}

-(id)initWithY:(CGFloat)y{
    self = [self initAlert];
    if(self){
        [self setStartWithY:y];
    }
    return self;
}

-(id)initWithTitle:(NSString *)title{
    self = [self initAlert];
    if(self){
        [self initContentWithTitle:title];
    }
    return self;
}

-(id)initAlert{
    self = [self initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width, Y_OFFSET_ALERT, WIDTH_ALERT, HEIGHT_ALERT)];
    if(self){
        self.y = Y_OFFSET_ALERT;
        self.backgroundColor = [UIColor blackColor];
        [self setAlpha:0.85];
        self.isOnLeft = NO;
    }
    return self;
}

-(void)setStartWithY:(CGFloat)y{
    self.y = y;
    self.frame = CGRectMake([UIScreen mainScreen].bounds.size.width, self.y, WIDTH_ALERT, HEIGHT_ALERT);
}

-(void)setTitle:(NSString *)title{
    self.string_title = [NSString stringWithString:title];
    self.lb_title.text = self.string_title;
}

- (void)initContentWithTitle:(NSString *)title{
    self.string_title = [NSString stringWithString:title];
    self.lb_title = [[UILabel alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width + GAP_DEFAULT, (HEIGHT_ALERT - HEIGHT_TITLE) / 2 + self.y, WIDTH_ALERT, HEIGHT_TITLE)];
    self.lb_title.text = self.string_title;
    self.lb_title.textColor = [UIColor whiteColor];
    self.lb_title.font = FONT_TITLE;
}

-(void)showWithCover:(BOOL)boolean{
    CGRect rect_cover = CGRectZero;
    if(boolean){
        rect_cover = [UIScreen mainScreen].bounds;
    }
    self.btn_cover = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btn_cover setFrame:rect_cover];
    [self.btn_cover setBackgroundColor:[UIColor clearColor]];
    [self.btn_cover addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [self.btn_cover addSubview:self];
    [self.btn_cover addSubview:self.lb_title];

    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self.btn_cover];

    [UIView animateWithDuration:0.35f animations:^{
        [self setFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - WIDTH_ALERT, self.y, WIDTH_ALERT, HEIGHT_ALERT)];
        [self.lb_title setFrame:CGRectMake(self.frame.origin.x + GAP_DEFAULT, (HEIGHT_ALERT - HEIGHT_TITLE) / 2 + self.y, WIDTH_ALERT, HEIGHT_TITLE)];
    } completion:^(BOOL finished) {

    }];
}

-(void)dismiss{
    CGFloat endPoint;
    CGFloat duration;
    if(self.isOnLeft){
        duration = 0.5;
        endPoint = 0 - WIDTH_ALERT;
    }else{
        duration = 0.3;
        endPoint = [UIScreen mainScreen].bounds.size.width;
    }
    [UIView animateWithDuration:duration animations:^{
        [self setFrame:CGRectMake(endPoint, Y_OFFSET_ALERT, WIDTH_ALERT, HEIGHT_ALERT)];
        [self.lb_title setFrame:CGRectMake(self.frame.origin.x, (HEIGHT_ALERT - HEIGHT_TITLE) / 2 + self.y, WIDTH_ALERT, HEIGHT_TITLE)];
    } completion:^(BOOL finished) {
        [self.btn_cover removeFromSuperview];
        [self.lb_title removeFromSuperview];
    }];
}

-(void)setTitleColor:(UIColor *)titleColor{
    self.color_title = titleColor;
    self.lb_title.textColor = self.color_title;
}

-(void)setAlertColor:(UIColor *)alertColor{
    self.color_background = alertColor;
    self.backgroundColor = self.color_background;
    [self setAlpha:0.9];
}

-(void)setAlertColor:(UIColor *)alertColor andTitleColor:(UIColor *)titleColor{
    [self setTitleColor:titleColor];
    [self setAlertColor:alertColor];
}

-(void)setDismissOnLeft:(BOOL)isOnLeft{
    self.isOnLeft = isOnLeft;
}

@end

要是把新建多少个新的Cocoa Touch Class类,然后把地点代码复制进行就好了。
在测试在此以前,大家还必要实现UITableView的点击响应事件。

RootViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%s || section: %ld and row: %ld of cell's been selected.", __FUNCTION__, indexPath.section, indexPath.row);
    [self showAlertWithSection:indexPath.section andRow:indexPath.row];
}

- (void)showAlertWithSection:(NSInteger)section andRow:(NSInteger)row{
    NSString *string_temp = [NSString stringWithFormat:@"%@ - %@", self.array_section[section], [self.array_row[section] objectAtIndex:row]];
    AlertMessage *alert = [[AlertMessage alloc]initWithTitle:string_temp];
    [alert setAlertColor:[CommonHandler getColorWithRGB:0xFFE4C4]];
    [alert showWithCover:YES];
}

在底下加上那七个方法,就达成了。今后就可以测试下效果了!

AlertMessage.gif

跳转页面


由于岁月涉及,此次就先跳二个空荡荡的页面好了!
咱俩先Command+N新建二个空手的UIViewController。
以此类小编打算作为一而再项目插件,所以就叫他为RouteViewController好了。
先河入刚新建的公文到RootViewController里面,然后修改initData,新加1个分组。然后再在didSelectedRowAtIndexPath:里面改改就完毕了。

RootViewController.m
#import "RouteViewController.h"
...
- (void)initData{
    self.array_section = [NSArray arrayWithObjects:@"自定义消息提醒", @"自定义路线插件", nil];

    self.array_row = [NSArray arrayWithObjects:
                      [NSArray arrayWithObjects:@"Show Alert", nil],
                      [NSArray arrayWithObjects:@"Show View", nil],
                      nil];
}
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%s || section: %ld and row: %ld of cell's been selected.", __FUNCTION__, indexPath.section, indexPath.row);
    switch (indexPath.section) {
        case 0:
            [self showAlertWithSection:indexPath.section andRow:indexPath.row];
            break;
        case 1:
            [self gotoRouteView];
            break;
        default:
            break;
    }
}
...
- (void)gotoRouteView{
    RouteViewController *instance_routeView = [[RouteViewController alloc]init];
    [self.navigationController pushViewController:instance_routeView animated:YES];
}

先起首化RouteViewController,然后经过UINavigationController里面的pushViewController方法来将RouteViewController放到栈顶,并展现到显示器上。
<br />

补给有些


以前对RootViewController的卷入的时候,因为马虎,还有2个地点没有打包好,以往补上来:

RootViewController.m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.array_row[section] count];
}

那就是说今后就来探视跳转页面包车型大巴功用呢!是或不是很愿意啊!笔者也是呀!

ShowView.gif

深信不疑都来看了,在跳转页面之后,是一片灰(其实是空内容),固然初步化了RouteViewController,可是并从未在RouteViewController里面写任何代码,所以就相会世那样的魔法。
<br />

睡眠此前


今日就暂到此地,前几天就从头写一下Route插件!祝福小编。

相关文章

网站地图xml地图