ios9API基础知识总结(二)

 UIAlertView(警告框)

 

 

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"标题" message:@"是否允许访问通讯录" delegate:self cancelButtonTitle:@"NO"otherButtonTitles:@"YES", nil];

   

设置警告框的样式(具体看enum)

alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;

2.显示警告框

[alert show];

 

 UIActionSheet

 

  //1.创建action实例

    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"标题" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"破坏" otherButtonTitles:@"微博",@"微信", nil];

   

    //2.显示

    [sheet showInView:self.view];

 

 

 

 

 

 

 

 

导航控制器

 

在AppDelegate.m文件中的

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

方法中创建一个导航控制器

 

//创建一个管理avc的导航控制器

    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:avc];

 

//1.创建BVC的实例

BViewController *bvc=[[BViewController alloc]initWithNibName:@"BViewController" bundle:nil];

//2.由管理着当前avc的上面的导航控制器负责推出 下一个vc界面

    [self.navigationController pushViewController:bvc animated:YES];

 

//返回到上一个界面

    [self.navigationController popViewControllerAnimated:YES];

 

 

 

 

 

 

 多界面切换

 

 

//1.创建SecondViewController的实例

SecondViewController *vc = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];

//2.推出显示vc

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

 

// 从哪个vc present过来的,dismiss就自动

// 回到哪个vc

[self dismissViewControllerAnimated:YES completion:nil];

 

 

正向传值

 

step1:为B公开一个属性。在.h文件中

// 公开一个属性,用于接收被传入的数据

@property(nonatomic,strong)NSString *strInfo;

step2:在A推出B之前,为B公开的属性赋值即可

// 1.创建BVC的实例

BViewController *bvc = [[BViewController alloc]initWithNibName:@"BViewController" bundle:nil];

   

// 2.为公开的属性赋值(传值)

 bvc.strInfo = self.textField.text;

// 3.推出bvc

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

   [self.navigationController pushViewController:bvc animated:YES];

step3:在B显示界面之前,将属性中的值显示在界面上

// 显示strInfo属性中的内容

    self.resultLabel.text = self.strInfo;

 

 

 

 

反向传值

 

方案一

step1:为B公开一个可以存储A引用的属性//一定是weak属性

// 公开一个可以存储返回到哪个vc的属性

@property(nonatomic,weak)AViewController *returnVC;

step2:为A公开一个可以存储传入的数据的属性

// 公开一个属性,存储B返回的文字内容

@property(nonatomic,strong)NSString *returnStr;

step3:A在推出B之前,将自己的引用传给B

 // 1.创建BVC的实例

    BViewController *bvc = [[BViewController alloc]initWithNibName:@"BViewController" bundle:nil];

   

    // 2.将自己的引用传给bvc

    bvc.returnVC = self;

   

    // 3.推出bvc

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

}

step4:B在返回到A之前,借助自己保存的A的引用,为A赋值

[self.returnVC showReturnMessage:self.textField.text];

step5:A要在恢复显示时,将接收到的数据显示在界面上

- (void)showReturnMessage:(NSString *)str

{

    self.resultLabel.text = str;

}

 

方案二:委托模式

委托方的三件事:

       step1:定义协议

//定义协议

/*

 1.协议名:类名+Delegate 

 2.方法的第一个参数 一定是委托方自己

 3.方法名尽量体现发消息的时机

*/

 

//1.定义协议

@protocol BViewControllerDelegate <NSObject>

 

-(void)bViewController:(BViewController *)bvc didInputString:(NSString *)str;

 

@end

       step2:公开一个属性叫代理delegate

@interface BViewController : UIViewController

 

//2.公开一个代理属性

@property(nonatomic,weak)id<BViewControllerDelegate> delegate;

 

@end

       step3:合适的时机给代理发消息

// 点击按钮后返回到avc

- (IBAction)goback:(id)sender {

   

    // 3.合适的时机给代理发消息

    [self.delegate bViewController:self didInputString:self.textField.text];

   

    [self dismissViewControllerAnimated:YES completion:nil];

}

 

代理方的三件事:

       step1:遵守协议

@interface AViewController ()<BViewControllerDelegate>

       step2:实现方法

//2.实现方法

- (void)bViewController:(BViewController *)bvc didInputString:(NSString *)str

{

    // 发回消息后显示文本

    self.label.text = str;

}

       step3:把自己设置为代理

    //3.将自己设置为bvc的代理,好接收返回的消息

    bvc.delegate = self;

 

 导航栏设置

 

 // 1.配置导航栏的右侧按钮

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(clickEdit:)];

//1.5配置按钮的功能为推出下一个界面

- (void)clickEdit:(UIBarButtonItem *)item

{

    // 推出灰色的界面

    OtherViewController *vc = [[OtherViewController alloc]initWithNibName:@"OtherViewController" bundle:nil];

    // 设置要推出的vc隐藏底部各种bar

    //vc.hidesBottomBarWhenPushed = YES;

 

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

   

    // 如果是导航控制器之间的切换 写以下代码

    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:vc];

    [self.navigationController presentViewController:navi animated:YES completion:nil];

   

}

 

 

 

 

// 2.配置左侧按钮

    UIBarButtonItem *item4 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"wifi_mid"] style:UIBarButtonItemStylePlain target:nil action:nil];

    self.navigationItem.leftBarButtonItem = item4;

   

// 3.配置中间的标题

    self.navigationItem.title = @“文字";

 

// 4.设置导航栏中间视图为创建好的按钮对象

    self.navigationItem.titleView = button;

// 对于系统的titleView区域是有指定的位置的,所以按钮的x和y这两个值

    // 系统根本不会去使用,设置任何值

    // 都会使得按钮出现在导航栏的中间位置

    button.frame = CGRectMake(0, 0, 100, 40);

    //button.backgroundColor = [UIColor redColor];

    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [button setTitle:@"标题按钮" forState:UIControlStateNormal];

    //设置按钮正常状态下的图片

    [button setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];

    // 设置按钮在选中状态下的图片

    [button setImage:[UIImage imageNamed:@"up"] forState:UIControlStateSelected];

   

    [button addTarget:self action:@selector(clickTitleButton:) forControlEvents:UIControlEventTouchUpInside];

-(void)clickTitleButton:(UIButton *)sender

{

    // 修改sender的状态

    sender.selected = !sender.selected;

}

 

// 配置工具条显示

    self.navigationController.toolbarHidden = NO;

    // 配置工具条中的按钮

    UIBarButtonItem *tItem1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:nil action:nil];

    UIBarButtonItem *tItem2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRewind  target:nil action:nil];

    UIBarButtonItem *tItem3 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:nil action:nil];

    // 木棍特效按钮

    UIBarButtonItem *fixItem =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

    // 设置木棍的长度

    fixItem.width = 40;

   

    // 弹簧特效按钮

    UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    self.toolbarItems = @[fixItem,tItem2,flexItem,tItem1,flexItem,tItem3,fixItem];

 

 

 

 

// 设置导航栏隐藏

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    //self.navigationController.navigationBarHidden = !self.navigationController.navigationBarHidden;

    [self.navigationController setNavigationBarHidden:!self.navigationController.navigationBarHidden animated:YES];

}

 

 

 

 

图片控件

 

// 1.创建ImageView

    UIImageView *imageView = [[UIImageView alloc]init];

// 2.设置图片控件内容

  imageView.image = [UIImage imageNamed:@"fbb03.jpg"];   

// 3.设置图片显示模式

    imageView.backgroundColor = [UIColor redColor];

    imageView.contentMode = UIViewContentModeScaleAspectFill;    

 // 4.设置图片控件的位置及大小

    imageView.frame = CGRectMake(110, 100, 50, 250); 

 // 5.添加到self.view中

    [self.view addSubview:imageView];

 

 

 

 

滚动视图

 

 

// 1.创建滚动视图的实例

    UIScrollView *sv = [[UIScrollView alloc]init];

    self.sv = sv;

   

// 2.设置sv的属性

    // bounds:代表视图的大小,其中默认 x和y都是0

    sv.frame = self.view.bounds;

   

    // 向sv添加要显示的子内容

    // 备注:如果是在创建图片视图的同时就指定了图片

    // 那么图片视图会默认与图片保持相等的大小

    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"desktop.jpg"]];

    self.iv = imageView;

    [sv addSubview:imageView];

   

    // 设置滚动视图的内容大小

    sv.contentSize = imageView.frame.size;

   

    /* 缩放有关的设置 */

    sv.maximumZoomScale = 1;

    CGFloat xScale = sv.frame.size.width/imageView.frame.size.width;

    CGFloat yScale = sv.frame.size.height/imageView.frame.size.height;

    sv.minimumZoomScale = MIN(xScale, yScale);

    //设置滚动视图的代理,意在说明,如果滚动视图上

    //有任何的变化行为,则交给当前控制器来处理

    sv.delegate = self;

调用代理后调用此方法

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

{

    return self.iv;

}

 

 

 

//点击 移动 按钮后,切换滚动视图的显示区域,需要有按钮且调用此方法

-(void)move

{

    [self.sv setContentOffset:CGPointMake(860, 100) animated:YES];

}

 

 

 创建欢迎界面!!

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];   

    //1.创建滚动实例

    UIScrollView *scrollView = [[UIScrollView alloc]init];

    scrollView.frame = self.view.bounds;

    //1.5设置滚动视图的代理

    scrollView.delegate = self;

    //2.添加滚动视图

    [self.view addSubview:scrollView];

    //2.5配置便于不可以弹跳

    scrollView.bounces = NO;

    //2.6设置整页滚动

    scrollView.pagingEnabled = YES;

    //2.7设置滚动条不可见

    scrollView.showsHorizontalScrollIndicator = NO;

    //3.配置滚动视图

    //大小中竖直方向不想滚动则可以设置为0

    scrollView.contentSize = CGSizeMake(self.view.frame.size.width*imageCount,0);

    //4.向滚动视图内添加子视图

    for (int i=0; i<imageCount; i++)

    {

        UIImageView *imageview = [[UIImageView alloc]init];

        //判定系统尺寸选择图片

        //获取当前屏幕尺寸

        CGSize size = [UIScreen mainScreen].currentMode.size;

        BOOL isIP6 = CGSizeEqualToSize(size, CGSizeMake(750, 1334));

        NSString *imageName = nil;

        if (isIP6)

        {

            imageName = [NSString stringWithFormat:@"cm2_guide_step%d-ip6",i+1];;

        }

        else

        {

            imageName = [NSString stringWithFormat:@"cm2_guide_step%d",i+1];

        }

        imageview.image = [UIImage imageNamed:imageName];

        imageview.frame = CGRectMake(i*self.view.frame.size.width,0, self.view.frame.size.width, self.view.frame.size.height);       

        if (i==imageCount-1)

        {

            [self setupEnterButton:imageview];

        }

        [scrollView addSubview:imageview];

    }

    //5.配置“点” UIpageControl

    UIPageControl *pageControl = [UIPageControl new];

    self.pageControl =pageControl;

    //5.1配置“点”的数量

    pageControl.numberOfPages = 5;

    //5.2配置圆点的位置

    pageControl.frame = CGRectMake(0, self.view.frame.size.height-60, self.view.frame.size.width, 40);

    //5.3设置圆点没有被选中时的颜色

    pageControl.pageIndicatorTintColor = [UIColor redColor];

    //5.4设置圆点被选中时的颜色

    pageControl.currentPageIndicatorTintColor = [UIColor greenColor];

    //5.5禁止圆点与用户交互

    pageControl.userInteractionEnabled = NO;

    //5.6修改被选中的圆点的位置

    //pageControl.currentPage =2;   

    //6.将分页控件添加到self.view中

    [self.view addSubview:pageControl];

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    NSLog(@"欢迎");

 }

#pragma  mark - 滚动视图协议中的方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    CGPoint offset = scrollView.contentOffset;

    //q取滚动的横向距离与屏幕宽度的比值的整数倍

    int x= (offset.x) / self.view.frame.size.width+0.5;  

    //将这个整数倍作为被选中的小圆点的下标

    self.pageControl.currentPage = x;

    }

//配置最后一页的进入应用按钮

-(void)setupEnterButton:(UIImageView *)imageView

{

    //预备:开启图片的用户交互功能

    imageView.userInteractionEnabled = YES;

    UIButton *button = [[UIButton alloc]init];

    button.frame = CGRectMake((self.view.frame.size.width-150)/2, self.view.frame.size.height*0.75, 150, 40);

    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    button.backgroundColor = [UIColor colorWithRed:216/255.0 green:51/255.0 blue:44/255.0 alpha:1];

    [button setTitle:@"欢迎进入" forState:UIControlStateNormal];

    button.titleLabel.font = [UIFont boldSystemFontOfSize:17];

    [button addTarget:self action:@selector(enterAPP) forControlEvents:UIControlEventTouchUpInside];

    [imageView addSubview:button];

}

//设置按钮事件

//默认图片是不能与用户交互的,图片又是按钮的父视图,当用户点击屏幕时,开始查找点击的视图,由于图片视图屏蔽了操作,所以不会找带到imageView事件,所以事件也不会落到imageView的子视图上

-(void)enterAPP

{

 mainViewController *mainVc = [mainViewController new];

    //为了在进入主界面后welcome界面从内存中释放,所以需要更换window 的根视图为main

    UIWindow * window = [UIApplication sharedApplication].keyWindow;

    window.rootViewController = mainVc;

   

}

 

 

 

 

 

 表视图创建

 

 

// 1.创建表视图

    //UITableView *tv = [[UITableView alloc]init];

 

    // 2.设置表视图的位置

    //tv.frame = self.view.bounds;

   

    // 创建一个可以有多个分区的表视图

    UITableView *tv = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

   

    // 3.添加表视图到view中

    [self.view addSubview:tv];

   

    // 4.设置表视图的数据源代理

    tv.dataSource = self;

   

    // 5.为了响应用户与表格之间的交互,设置表视图的delegat代理

    tv.delegate = self;

   

 

三问一答

#pragma mark - 数据源协议UITableViewDataSource

 

/*

    实现表视图的数据展示总结为两个字:三问  

    第一问:有几个分区

    第二问:每个分区有几行

    第三问:每行什么样

*/

/* 问一:表格有多少个分区 */

/* 此方法为可选,不提供实现时,默认返回1 */

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    NSLog(@"numberOfSectionsInTableView:");

    return 3;

}

 

/* 问二:表格的每一个分区有多少行 */

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

{

    NSLog(@"numberOfRowsInSection:");

   

    if (section==0) {//第一个分区

        return 3;

    }else if (section == 1){//第二个分区

        return 2;

    }else {//第三个分区

        return 4;

    }

}

 

/* 问三:每一行什么样 */

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

{

    NSLog(@"cellForRowAtIndexPath:");

   

    UITableViewCell *cell = [[UITableViewCell alloc]init];

   

    if (indexPath.section == 0) {

        cell.textLabel.text = @"Hello";

    }else if (indexPath.section == 1){

        cell.textLabel.text = @"Hello World";

    }else{

        cell.textLabel.text = @"Hello Kitty";

    }

    return cell;

}

 

#pragma mark - 代理协议UITableViewDelegate

/* 一答:选中某一行之后做什么  */

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

{ NSLog(@"[section:%ld][row:%ld]",indexPath.section,indexPath.row);

}

 

 

 

 

/*

    实现表视图的数据展示总结为两个字:三问  

    第一问:有几个分区

    第二问:每个分区有几行

    第三问:每行什么样

*/

/* 问一:表格有多少个分区 */

/* 此方法为可选,不提供实现时,默认返回1 */

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    NSLog(@"numberOfSectionsInTableView:");

    return 3;

}

 

/* 问二:表格的每一个分区有多少行 */

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

{

    NSLog(@"numberOfRowsInSection:");

   

    if (section==0) {//第一个分区

        return 3;

    }else if (section == 1){//第二个分区

        return 2;

    }else {//第三个分区

        return 4;

    }

}

 

/* 问三:每一行什么样 */

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

{

    NSLog(@“cellForRowAtIndexPath:");

 

    //没有用单元格重用

    UITableViewCell *cell = [[UITableViewCell alloc]init];

   

    if (indexPath.section == 0) {

        cell.textLabel.text = @"Hello";

    }else if (indexPath.section == 1){

        cell.textLabel.text = @"Hello World";

    }else{

        cell.textLabel.text = @"Hello Kitty";

    }

    return cell;

}

 

 

 

#pragma mark - 代理协议UITableViewDelegate

/* 一答:选中某一行之后做什么  */

 

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

{

NSLog(@“[section:%ld][row:%ld]",indexPath.section,indexPath.row);

 

}

 2.设置单元格的高度

 */

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 80;

 

 

}

补充:

//1.**按下高亮 松手回到正常

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

{

  [tableView deselectRowAtIndexPath:indexPath animated:YES];

   

}

 

2.UITableViewController去掉多余的Cell

- (void)viewDidLoad {

    [super viewDidLoad];

   //**去掉多余的Cell

    self.tableView.tableFooterView = [UIView new];

}

3.UITablViewContller:的故事板中定义单元格(添加图片(即表头))的设置:

3.1 方式一:UITablViewContller:的故事板中选中tableView然后选第四个检查器可以prototype Cells是几就是几个单元格,然后再选Selection:为No Selection是系统自定义的,若要自己定义单元格1.就在连一个UITableViewCell的累,2.或者就在UITableViewController的#import "HeroDetailTableViewController.h"下面写上这个和创建一个类是一样的。

@interface HeroDetailCell : UITableViewCell

(定义属性的)

@end

 

@implementation HeroDetailCell

(写方法的)

@end

看【day05_0_昨天的作业】;

3.2方式二:或者在cell的第一单元格加一个View,上面在加UIImageViwe,和label,这个可以直接拉线到UITableViewController中。

看【day05作业英雄联盟界面】

 

 

4.在TableView中的单元格中设置分区,点tableView中的Style为Grouped,分组格式。Selection为No Selection是点击单元格不下陷,但是点击单元格上的按钮管用

设置section头部高度像素,高度最小是

-CGFloattableViewUITableView*tableViewheightForHeaderInSectionNSIntegersection

return

设置section脚步高度像素

-CGFloattableViewUITableView*tableViewheightForFooterInSectionNSIntegersection

 

return

row的高度

-CGFloattableViewUITableView*tableViewheightForRowAtIndexPathNSIndexPath*indexPath

*

CGFloatheightkWindowW*定义过宏



returnheight

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 UITableViewController表视图控制器

(特点:自带了一个tableView视图控制器已经遵守了两个协议控制器也已经是自带的表视图的代理只需要创建一个类继承UITableViewController和关注三问一答即可其他的可自动生成.

单元格重用,以前在第三问的时候一直都是  UITableViewCell  *cell = [[UITableViewCell alloc]init]创建cell,单元格重用节省资源,有两种方法,方式一:需要自己判断是否取到,没有取到时,自己新建cell的实例.方式二,系统创建,(可以提前和系统注册一种单元格,然后,回答第三问时,直接从队列中取即可,不再需要判断有没有取到了,因为,在取不到可重用的cell时,系统会自动根据提早注册的单元格,为我们新建))

//1.单元格重用(两种)

//1.1.第二种( 系统创建)

//加载的地方写(提前为表视图注册单元格,这样从队列中取不到可重用的。可重用的单元格时,系统就根据注册的cell的样式来自动新建)

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@“MyCell”];

 

//第三问的地方写( 只有上面注册过MyCell这个标识对应的类之后。 这句才能工作,系统才会在取不到cell时,自动创建cell)

 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];

 

//第一种(在第三问的地方写,可以定义格式)

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];

//判断有没有cell没有新建 

  if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@“MyCell”];//可以在里面重新定义cellc的内容视图

    }

 

内容视图:cell格式的四种:

(UITableViewCell

                   1)内容视图

                                               系统版:四种样式通过创建时的style参数设置

                                               自定义版:contentView中添加子视图

                   2)辅助视图

                                               系统版:accessoryType属性 ,四种样式

                                               自定义版:accessoryView属性)

 

1.UITableViewCellStyleDefault图和文  左右排布,无详情,

2.UITableViewCellStyleValue1 只有文字和详情,无图,详情左对齐

 

 

3.UITableViewCellStyleValue2  三部分都有,详情右对齐(right)

            

4.UITableViewCellStyleSubtitle三部分都有,详情在文字的下方

 

// 设置cell的内容(即文本)视图(contentView)(imageView等等系统自带的三个:)

 1. 核心内容的显示标签:

 cell.textLabel.text = @"Hello World”;

2.  详情的显示标签:

cell.detailTextLabel.text = @"详情内容";

3.图片显示视图:

cell.imageView.image = [UIImage imageNamed:@"icon40"];

   

2.向单元格里即开关

// 设置系统款的辅助视图样式

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

   

    // 让第三行的辅助视图区域出现一个switch开关

    if (indexPath.row == 2) {

        UISwitch *mySwitch = [[UISwitch alloc]init];

        [mySwitch setOn:YES];

        cell.accessoryView = mySwitch;

    }else{

        cell.accessoryView = nil;

    }

辅助视图:

3.添加单元格的辅助视图(四种:   UITableViewCellAccessoryDisclosureIndicator      大于号

cell.accessoryType = UITableViewCellAccessoryCheckmark    对勾                                                     UITableViewCellAccessoryDetailButton    圆圈i

                       UITableViewCellAccessoryDetailDisclosureButton   圆圈i+大于号

 

 

 

 // 设置系统款的辅助视图样式(第三问的地方写的)

 

 cell.accessoryType = UITableViewCellAccessoryNone;

 

//在下边写的方法

//点击cell的除 圆圈i 以外部分的响应

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

{

    NSLog(@"didSelectRowAtIndexPath");

}

 

//点击圆圈执行操作

// 只有cell中包含 圆圈i 时,点击圆圈i,则调用此方法

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

 

{ NSLog(@"accessoryButtonTappedForRowWithIndexPath");

}

 

 

 

 

4.*数组方法(即多个界面的传值):

(3.数据模型+表视图

         NSArray (NSString) 找到 数组元素的下标与行的row或section之间的关系

         NSArray(Object) 在推出的界面中显示数组的数据,需要正向传值

         NSArray(Object—NSArray))

(1.数组方法2.切换界面传值(其中前面有一个类City这个类还有一个数组(NSArray *allCitys)切换到另一个界面))

//懒加载

- (NSArray *)allCitys

{

    if (!_allCitys) {

        _allCitys = [City demoDate];

    }

   

    return _allCitys;

}

//第三问中写

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“Cell”];

 

  if (cell ==  nil) {   

 cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"];

    }(写第二种方法重用单元格)

 

// 设置cell有辅助视图

 

 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

 

 // 1.先按照当前行号做下标,找到该行要显示的对应的对象(数组方法)

 

 City *city = self.allCitys[indexPath.row];

   

 // 2.再显示对象中的各个属性到cell的指定位置

 

 cell.textLabel.text = city.name;

//显示人数

 //cell.detailTextLabel.text = [NSString stringWithFormat:@“%ld万人",city.population];

 

//选中行以后推出新的vc,显示选中的城市信息详情

(又写了一个方法切换到另一个界面上,即北京的有哪几个城市,由另几个界面推出,不用遵循协议。)

 

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

{

 // 1.创建详情vc的实例

   

DetailViewController *detailVC = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil]

 

//*** 2.将选中的行所对应的数据取出,传给推出的vc进行显示

 

 City *selectedCity = self.allCitys[indexPath.row];

 detailVC.city = selectedCity;

(其中由在另一个类中写了一个公开的属性City类的属性在.h中声明)

 

 // 3.推出vc(navigationController推出)

 

[self.navigationController pushViewController:detailVC animated:YES];

}

【Demo05 显示城市信息】

 

设置分区头、分区尾(在第三问一面写的带xib)

 

//自定义分区头,显示居中、红色、字号偏大的城市名称

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

{

 

    City *city = self.allCitys[section];

    UIView *headerView = [[UIView alloc]init];

    headerView.frame  = CGRectMake(0, 0, tableView.frame.size.width, 60);

    UILabel *label = [[UILabel alloc]init];

    label.frame = CGRectMake(0, 0, tableView.frame.size.width, 60);

    //label.backgroundColor = [UIColor redColor];

    label.font = [UIFont systemFontOfSize:24];

    label.textAlignment = NSTextAlignmentCenter;

    label.text = city.name;

   // return label;

     [headerView addSubview:label];

    //创建一个图片视图

    //将图片添加到headerView

//    UIImageView *imageView = [[UIImageView alloc]init];

//    imageView.image = [UIImage imageNamed:@"icon40"];

    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon40"]];

   

    [headerView addSubview:imageView];

   

    return headerView;

 

}

//定义分区头的高度

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

{

    return 80;

 

 

}

//设置每个分区的分区尾,显示这个城市的人口数

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

{

     //根据分区号找到对应的城市

    City *city = self.allCitys[section];

    //返回人口数

    return [NSString stringWithFormat:@"%ld万人",city.population];

 

}

 

 

 

 

 

 

 表格的删除、增加、移动

1.删除和添加:(两问一答)

表格的编辑模式

*开启编辑模式:editing 为YES

*删除/增加 :两问一答

*移动:一问一答

2.自定义单元格

         *自定义一个类,继承自UITableViewCell

                            *公开一个模型属性用于接收要显示的对象

                            *重写该属性的setter方法,完成赋值显示

                            *创建实例的过程封装到自定义类中

         *回答第三问时,创建自定义的cell的实例

                           

3.动态表格+静态表格

         静态表格:行数不变,但不代表数据不变,需要使用大量的硬编码 if + switch 实现每一个cell的设置

 

4.使用xib实现静态表格的显示

                   所有拖拽到xib上的视图都会由系统自动创建,并且xib中可以添加多个独立的视图

 

// 问一:该行是否可以编辑(选问)

 

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (indexPath.row == 0) {

        return NO;

    }else{

        return YES;

    }

}

// 问二:行是什么样式(选问)

 

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (indexPath.row == (self.allCitys.count-1)) {

        return UITableViewCellEditingStyleInsert;

    }else{

        return UITableViewCellEditingStyleDelete;

    }

    //return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;(显示一个小圆圈可以打对勾的执行删除或插入的样式)

}

 

//一答:点击加号或减号后的响应动作(必答)

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (editingStyle==UITableViewCellEditingStyleDelete) {//点击了删除按钮

        // 1.先删除数据模型中的数据(数组)

        [self.allCitys removeObjectAtIndex:indexPath.row];

        // 2.更新界面

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    }else{

        // 1.构建定值数据,添加到模型中

        City *newCity = [[City alloc]init];

        newCity.name = @"test";

        newCity.population = 1800;

        [self.allCitys addObject:newCity];

       

        // 2.更新界面

        NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:self.allCitys.count-1 inSection:0];

        [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    }

}

2.移动

 

//问一:当前行是否可以移动

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

    return YES;

}

 

//一答:移动后如何响应

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{

    // 1.先根据原始位置取出数组中的数据

    City *city = self.allCitys[sourceIndexPath.row];

   

    // 2.从原始位置移除

    [self.allCitys removeObject:city];

   

    // 3.将数据按照新的位置插入到数组中

    [self.allCitys insertObject:city atIndex:destinationIndexPath.row];

}

 

 

 

3.自定义单元格

    *自定义一个类,继承自UITableViewCell

           *公开一个模型属性用于接收要显示的对象

            *重写该属性的setter方法,完成赋值显示

        *创建实例的过程封装到自定义类中

    *回答第三问时,创建自定义的cell的实例

 

 

自定义表格的方法(看【demo03自定义单元格】新闻的例子)

(重新重建了一个类带xib的添加了两个UILbel 和一个UIImageView)

//cell自己为指定的表视图创建或dequeue一个实例对象(这是公开的方法在.h声明了,又公开@property(nonatomic,strong)News *news;属性,接收数据模型

// 在m文件中,由cell自己来决定这个对象如何显示)

 

+ (instancetype)cellForTableView:(UITableView *)tableView

{

    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" ];

    if (cell == nil) {

        //按照指定的xib的文件名加载界面,并创建

        //xib中的视图对象,但返回值是个数组类型

        //如果当前xib中只有一个视图,则取lastObject即可

        cell = [[[NSBundle mainBundle] loadNibNamed:@"NewsCell" owner:nil options:nil] lastObject];

    }

    return cell;

}

 

//重写news属性的setter方法

//只要外界已给cell赋值一个新闻对象,就立即完成对象

//中的各个属性显示到界面的视图中

- (void)setNews:(News *)news

{

    _news = news;

    // 将新闻显示到cell中

    self.titleLabel.text = news.title;

    self.newsImageView.image = [UIImage imageNamed:news.newsImage];

    self.commentCountLabel.text = [NSString stringWithFormat:@"%ld",news.commentCount];

}

 

在控制器中的第三问中写:

NewsCell *cell = [NewsCell cellForTableView:tableView];

 // 获取该位置对应的新闻对象

 News *news = self.allNews[indexPath.row];

  cell.news = news;

 

 

 

1.设置表格的行高(加载的地方写)

 self.tableView.rowHeight = 80;

 

2.设置分区之间的空隙大小

//空隙的大小

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

{

    return 5;

}

 

//3.1设置表头(NewsTableHearderView是xib自定义的即添加进来)

  self.tableView.tableHeaderView = [[[NSBundle mainBundle]loadNibNamed:@"NewsTableHearderView" owner:nil options:nil]lastObject];

//3.2设置表尾 (NewsTableFooterView同理是xib添加别的功能设置协议<NewsTableFooterViewDelegate>)

    NewsTableFooterView *footerView = [[[NSBundle mainBundle]loadNibNamed:@"NewsTableFooterView" owner:nil options:nil]lastObject];

   

  footerView.delegate = self;

  self.tableView.tableFooterView = footerView;

 

//在另一个类里写的他俩遵守协议

 

// 点击了加载按钮后响应(设置按钮与标签的隐藏)

- (IBAction)clickLoadButton:(id)sender {

 

    self.loadButton.hidden = YES;

    self.tipView.hidden = NO;

    // 给代理发消息

    [self.delegate newsTableFooterViewDidClickLoadButton:self];

}

 

// 当数据接收完毕时,做视图的切换(声明的一个方法)

-(void)didLoadNews

{

    self.loadButton.hidden = NO;

    self.tipView.hidden = YES;

}

 

静态表格:

按照显示的数据行数是否会发生变化,分两类

          1>动态表格:行数不定

                                      实现方式1:自己创建tableView,设置代理,添加到普通的vc中

                                       实现方式2:继承自UITableViewController,关注三问一答即可

                                      注意:一定要为这一组数据准备模型

         2>静态表格:行数固定不变(但不代表数据不变)

                                      主要应用场景:App中的设置功能

                                      实现特点:控制器中会出现大量的硬编码,会有很多的if  switch 这样的判断语句

        2.1>使用xib实现静态表格的显示

                   所有拖拽到xib上的视图都会由系统自动创建,并且xib中可以添加多个独立的视图

                                     

【Demo04_静态表格】;

 

 

 

原文地址:https://www.cnblogs.com/hytx/p/5053703.html