导航视图控制器

来源:互联网 发布:java 网络编程框架 编辑:程序博客网 时间:2024/06/10 00:01

APPDELEGATE

#import <UIKit/UIKit.h>

@interface AppDelegate :UIResponder <UIApplicationDelegate>

@property (retain,nonatomic)UIWindow *window;

@end


#import "AppDelegate.h"

#import "MainViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate

- (void)dealloc

{

    [_windowrelease];

    [superdealloc];

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

   

    self.window = [[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];

    [_windowsetBackgroundColor:[UIColorwhiteColor]];

    [_windowmakeKeyAndVisible];

    [_windowrelease];

    

    MainViewController *main = [[MainViewControlleralloc]initWithNibName:nilbundle:nil];

    UINavigationController *nav = [[UINavigationControlleralloc]initWithRootViewController:main];

    [_windowsetRootViewController:nav];

    [mainrelease];

    

    

    

    return YES;

}


MainViewController

#import <UIKit/UIKit.h>



@interface MainViewController :UIViewController

//数组为我们提供数据

@property(nonatomic,retain)NSMutableArray *tableArray;


@end


#import "MainViewController.h"

#import "CustomCell.h"

#import "SecondViewController.h"

@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>


@end


@implementation MainViewController


- (void)dealloc

{

    [_tableArrayrelease];//这个减的是属性对应的引用计数(+1)

    [superdealloc];

}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

   self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];

   if (self) {

         //初始化方法里一般不写跟视图相关的内容,只写跟数据相关的内容,eg:创建数组

        self.tableArray = [NSMutableArrayarray];//array是便利构造器,所以不需要释放

        NSMutableDictionary *dic1 = [NSMutableDictionarydictionary];

        [dic1setObject:@"孙树海"forKey:@"name"];

        [dic1setObject:@"110"forKey:@"tel"];

        [dic1setObject:@"蓝欧"forKey:@"address"];

        

        NSString *path = [[NSBundlemainBundle]pathForResource:@"1"ofType:@"jpg"];

       UIImage *image = [UIImageimageWithContentsOfFile:path];

        [dic1setObject:imageforKey:@"photo"];

        [_tableArrayaddObject:dic1];

        

        //一旦数组元素发生改变,要看对行数有没有影响

        //数据数据 把字典传过去显示全部信息即可

        

        

    }

    return self;

}

- (void)viewDidLoad {

    [superviewDidLoad];

    

    

    [selfcreateTableView];

    

    

}

- (void)createTableView

{

    //导航栏透明度设置为不透明

    self.navigationController.navigationBar.translucent = NO;

    //创建一个tableview

    UITableView *table = [[UITableViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height - 64) style:UITableViewStylePlain];

    [table setBackgroundColor:[UIColorcyanColor]];

    //分割线的样式,颜色

    //table.separatorStyle = UITableViewCellSeparatorStyleNone;//没有线

    table.separatorStyle =UITableViewCellSeparatorStyleSingleLine;

    table.separatorColor = [UIColorgrayColor];

    table.rowHeight =400;

    [self.viewaddSubview:table];

    [tablerelease];

    

    //分割线是假的,看着有线,其实没有,由我们来定行数和内容

    //协议的两大作用:1.监听 2.设置

    

    //我们需要给tableView指定一个数据源,它负责给tableView提供数据

    //注意代理人名称是dataSource,不是delegate

    table.dataSource =self;

    table.delegate =self;

    

    

}

//这两个协议只是用来取内容,数据都是在外界创建

#warning 告诉tableview中第几个section中有几行,section默认一个

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

//   //分区里有几行

//    NSLog(@"%s",__func__);

//   //数组的个数和他显示的行数一定要是一一匹配的

    return [_tableArraycount];//返回行数

    

    //model继承与NSObject

    

    

}

#warning 给每一个row赋值一个cell,每出现一个新的行,就会走下面的方法,来给这一新的行赋值内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    //1.定义重用池的名字,

   staticNSString *cellIdentify =@"cell1";

    //2.从重用池中取不用的cell,看能不能取出来,

    //从以cell1命名的重用池里取

   CustomCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellIdentify];

    //如果没有,新建一个cell,并放到重用池cell1

   if (!cell) {

        cell = [[[CustomCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellIdentify]autorelease];//要加autorelease

        

    }

    //3.设置背景颜色,if外面

    [cell setBackgroundColor:[UIColorgreenColor]];

  

//    NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpg"];

//    UIImage *image = [UIImage imageWithContentsOfFile:path];

//    cell.imageView.image = image;

//    

//    //图片标签

//    cell.textLabel.text = @"1";

//    //副标签

//    cell.detailTextLabel.text = @"2";

//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//加右侧箭头

    

    

    

    

    //不用系统的,因为不能满足要用的需求,所以用自定义的cell,定义好了之后要给外界提供接口,为其赋值

   // cell.label.text = @"千与千寻";

    //2.隐藏断点

    //3.跳过断点

    //4.走到下一行

    

    //对象po出来,一旦是nil,对象一定没有初始化或赋值或开辟空间

    

    //以后一旦创建一个tableView,就要创建一个数组和他对应,一个tableView对应一个数组

    

    //取数组里的值

//    NSString *name = [_tableArray objectAtIndex:indexPath.row];

//    cell.label.text = name;

    

    //取字典里的值

   NSDictionary *dic = [_tableArrayobjectAtIndex:indexPath.row];

   NSString *name = [dicobjectForKey:@"name"];

    cell.label.text = name;

    

   NSString *address = [dicobjectForKey: @"address"];

    cell.label2.text = address;

    

    

   NSString *tel = [dicobjectForKey:@"tel"];

    cell.tel.text = tel;

    

   UIImage *image = [dicobjectForKey:@"photo"];

    cell.photo.image = image;

    

   return cell;//返回cell

#warning 明天限时代码创建tableView及实现上面那两个协议,数组里的格式是字典(两个), cell自定义(一个label),没有代码提示


}


#warning 点击触发事件

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

{

    

    //indexPath的两个属性

//    NSLog(@"%ld",indexPath.section);

//    NSLog(@"%ld",indexPath.row);

    

    //取消选中状态

    //[tableView deselectRowAtIndexPath:indexPath animated:YES];

    //取值 

   NSDictionary *dictionary = [_tableArrayobjectAtIndex:indexPath.row];

    //创建第二个界面,跳过去

    SecondViewController *sec = [[SecondViewControlleralloc]init];

    sec.dic = dictionary;

    

   // [self.navigationController pushViewController:sec animated:YES];

    [secrelease];

    

    

}



SecondViewController

#import <UIKit/UIKit.h>


@interface SecondViewController :UIViewController

//创建一个字典,来接收第一页传过来的(字典)

@property(nonatomic,retain)NSDictionary *dic;


@end

#import "SecondViewController.h"


@interface SecondViewController ()


@end


@implementation SecondViewController

//布局固定,相册传名字就可以了

- (void)dealloc

{

    [_dicrelease];

    [superdealloc];

}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

   self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];

   if (self) {

        

    }

    return self;

}


- (void)viewDidLoad {

    [superviewDidLoad];

   

    //作业:通讯录 8个人

    //第一页:数组套字典

    //第二页显示详情(数据全是从上一页传过来的) :必须有头像(),名字,电话,地址,email(QQ邮箱)......

    //[扩展]:图片列表,上面是基本信息,下面是相册

    //cell上放scrollView,可以左右滚动

    //导航栏:返回按钮,用自己定义的(酷酷的图片),不要用系统的

    

    

    

}


CustomCell

#import <UIKit/UIKit.h>

//自定义cell   要往上添什么(label,imageview,image...)就把什么定义成属性

//cell本身就是view,可以往上面加东西

//textlabel detailTextLabel accessoryType

@interface CustomCell :UITableViewCell

//因为要给外界一个接口为label赋值  labelcell

@property(nonatomic,retain)UILabel *label;

@property(nonatomic,retain)UILabel *label2;

@property(nonatomic,retain)UILabel *tel;

@property(nonatomic,retain)UIImageView *photo;


@end

#import "CustomCell.h"


@implementation CustomCell


- (void)dealloc

{

    [_labelrelease];

    [_label2release];

    [_telrelease];

    [_photorelease];

    [superdealloc];

}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

   self = [superinitWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier];

   if(self){

       //用属性的话,属性是全局变量,用系统定义的是局部变量,在除self的其他地方不能用!!!!

       self.label = [[UILabelalloc]initWithFrame:CGRectMake(200,0,80, 40)];

        [_labelsetBackgroundColor:[UIColorclearColor]];

        //对于cell来说,添加到contentView

        [self.contentViewaddSubview:_label];

        [_labelrelease];

        

       self.label2 = [[UILabelalloc]initWithFrame:CGRectMake(200,60,80, 40)];

        [_label2setBackgroundColor:[UIColorclearColor]];

        [self.contentViewaddSubview:_label2];

        [_label2release];

        

       self.tel = [[UILabelalloc]initWithFrame:CGRectMake(200,120,80, 40)];

        [_telsetBackgroundColor:[UIColorclearColor]];

        [self.contentViewaddSubview:_tel];

        [_telrelease];

       

        

       self.photo = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,100, 100)];

        [self.contentViewaddSubview:_photo];

        [_photorelease];

        

        

        

        //cellimageview是只读类型,但可以给他的属性赋值


       

        

    }

    return self;

}









0 0
原创粉丝点击