ios开发(表视图)

1.在对象库中拖拉tableview至场景中。再拖拉tableviewcell作为原型单元格,注意要设置原型单元格标识符以备后面代码实现使用

2.设置tableVIEW属性(content:Dynamic Prototypes style:grouped),原型单元格属性(style:basic,image,identifier标识符,identation:1)

3.将tableview的输出口delegate和datasource链接至viewcontroller

4.实现逻辑(书本例子)

在Viewcontroller.H中添加协议

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

在.H中声明数组属性

@property (nonatomic, strong) NSArray *redFlowers;
@property (nonatomic, strong) NSArray *blueFlowers;

同时不要忘记清理

在.M中

- (void)viewDidUnload
{
    [self setRedFlowers:nil];
    [self setBlueFlowers:nil];
    [super viewDidUnload];

}

首先在viewcontroller..M中定义,避免程序出现magicnumber

#define kSectionCount 2
#define kRedSection 0
#define kBlueSection 1

在视图加载时填充原始数组

 1 - (void)viewDidLoad
 2 {
 3     self.redFlowers = [[NSArray alloc]
 4                        initWithObjects:@"Gerbera",@"Peony",@"Rose",
 5                        @"Poppy",nil];
 6     self.blueFlowers = [[NSArray alloc]
 7                         initWithObjects:@"Hyacinth",@"Hydrangea",
 8                         @"Sea Holly",@"Phlox",@"Iris",nil];
 9
10     [super viewDidLoad];
11     // Do any additional setup after loading the view, typically from a nib.
12 }

实现协议

返回分区数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return kSectionCount;
}
返回每个分区行数

- (NSInteger)tableView:(UITableView *)tableView
    numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case kRedSection:
            return [self.redFlowers count];
        case kBlueSection:
            return [self.blueFlowers count];
        default:
            return 0;
    }
}
每个分区标题

- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section {
    switch (section) {
        case kRedSection:
            return @"Red";
        case kBlueSection:
            return @"Blue";
        default:
            return @"Unknown";
    }
}
配置表视图单元格

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:@"flowerCell"];        //flowercell为之前界面设计时原始单元格的标识符
    
    
    switch (indexPath.section) {
        case kRedSection:
            cell.textLabel.text=[self.redFlowers
                                 objectAtIndex:indexPath.row];         //将单元格标签设置为花名
            break;
        case kBlueSection:
            cell.textLabel.text=[self.blueFlowers
                                 objectAtIndex:indexPath.row];
            break;
        default:
            [email protected]"Unknown";
    }
    
    UIImage *flowerImage;
    flowerImage=[UIImage imageNamed:
                 [NSString stringWithFormat:@"%@%@",
                  cell.textLabel.text,@".png"]];         //花图片
    cell.imageView.image=flowerImage;
    
    return cell;
}

响应单元格选择事件

 1 - (void)tableView:(UITableView *)tableView
 2             didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 3
 4     UIAlertView *showSelection;
 5     NSString    *flowerMessage;
 6
 7     switch (indexPath.section) {
 8         case kRedSection:
 9             flowerMessage=[[NSString alloc]
10                            initWithFormat:
11                            @"You chose the red flower - %@",
12                            [self.redFlowers objectAtIndex: indexPath.row]];
13             break;
14         case kBlueSection:
15             flowerMessage=[[NSString alloc]
16                            initWithFormat:
17                            @"You chose the blue flower - %@",
18                            [self.blueFlowers objectAtIndex: indexPath.row]];
19             break;
20         default:
21             flowerMessage=[[NSString alloc]
22                            initWithFormat:
23                            @"I have no idea what you chose!?"];
24             break;
25     }
26
27     showSelection = [[UIAlertView alloc]
28                      initWithTitle: @"Flower Selected"
29                      message:flowerMessage
30                      delegate: nil
31                      cancelButtonTitle: @"Ok"
32                      otherButtonTitles: nil];
33     [showSelection show];
34 }

创建基于主从视图应用程序

1.使用模板Master-Detail Application 新建项目,删除一个split view controller 一个negavationcontroller

2.将其修复IPHONE切换,按住CONTROL,选中单元格(不是表)连接到详细信息场景,选择PUSH链接

3.在detail页面用WEB视图填充满,显示网页信息。将标签detail view content goes here 放到WEB视图后面

因为这行字在IPHONE中永远也看不到,却在模板中也删不掉,所以放后面

4.实现数据源

在masterviewcontroller.H中

@property (strong, nonatomic) NSArray *flowerData;
@property (strong, nonatomic) NSArray *flowerSections;

同时清理

在maserViewCOntroller.M中

-viewdidunload中

1 [self setFlowerData:nil];
2     [self setFlowerSections:nil];

再master.H中声明方法将数据加入到数组

-(void)createFLowerData

 1 - (void)createFlowerData {
 2
 3     NSMutableArray *redFlowers;
 4     NSMutableArray *blueFlowers;
 5
 6     self.flowerSections=[[NSArray alloc] initWithObjects:
 7                     @"Red Flowers",@"Blue Flowers",nil];
 8
 9     redFlowers=[[NSMutableArray alloc] init];
10     blueFlowers=[[NSMutableArray alloc] init];
11
12     [redFlowers addObject:[[NSDictionary alloc]
13                            initWithObjectsAndKeys:@"Poppy",@"name",
14                            @"poppy.png",@"picture",
15                            @"http://en.wikipedia.org/wiki/Poppy",@"url",nil]];
16     [redFlowers addObject:[[NSDictionary alloc]
17                            initWithObjectsAndKeys:@"Tulip",@"name",
18                            @"tulip.png",@"picture",
19                            @"http://en.wikipedia.org/wiki/Tulip",@"url",nil]];
20     [redFlowers addObject:[[NSDictionary alloc]
21                            initWithObjectsAndKeys:@"Gerbera",@"name",
22                            @"gerbera.png",@"picture",
23                            @"http://en.wikipedia.org/wiki/Gerbera",@"url",nil]];
24     [redFlowers addObject:[[NSDictionary alloc]
25                            initWithObjectsAndKeys:@"Peony",@"name",
26                            @"peony.png",@"picture",
27                            @"http://en.wikipedia.org/wiki/Peony",@"url",nil]];
28     [redFlowers addObject:[[NSDictionary alloc]
29                            initWithObjectsAndKeys:@"Rose",@"name",
30                            @"rose.png",@"picture",
31                            @"http://en.wikipedia.org/wiki/Rose",@"url",nil]];
32     [redFlowers addObject:[[NSDictionary alloc]
33                            initWithObjectsAndKeys:@"Hollyhock",@"name",
34                            @"hollyhock.png",@"picture",
35                            @"http://en.wikipedia.org/wiki/Hollyhock",
36                            @"url",nil]];
37     [redFlowers addObject:[[NSDictionary alloc]
38                            initWithObjectsAndKeys:@"Straw Flower",@"name",
39                            @"strawflower.png",@"picture",
40                            @"http://en.wikipedia.org/wiki/Strawflower",
41                            @"url",nil]];
42
43     [blueFlowers addObject:[[NSDictionary alloc]
44                             initWithObjectsAndKeys:@"Hyacinth",@"name",
45                             @"hyacinth.png",@"picture",
46                             @"http://en.m.wikipedia.org/wiki/Hyacinth_(flower)",
47                             @"url",nil]];
48     [blueFlowers addObject:[[NSDictionary alloc]
49                             initWithObjectsAndKeys:@"Hydrangea",@"name",
50                             @"hydrangea.png",@"picture",
51                             @"http://en.m.wikipedia.org/wiki/Hydrangea",
52                             @"url",nil]];
53     [blueFlowers addObject:[[NSDictionary alloc]
54                             initWithObjectsAndKeys:@"Sea Holly",@"name",
55                             @"sea holly.png",@"picture",
56                             @"http://en.wikipedia.org/wiki/Sea_holly",
57                             @"url",nil]];
58     [blueFlowers addObject:[[NSDictionary alloc]
59                             initWithObjectsAndKeys:@"Grape Hyacinth",@"name",
60                             @"grapehyacinth.png",@"picture",
61                             @"http://en.wikipedia.org/wiki/Grape_hyacinth",
62                             @"url",nil]];
63     [blueFlowers addObject:[[NSDictionary alloc]
64                             initWithObjectsAndKeys:@"Phlox",@"name",
65                             @"phlox.png",@"picture",
66                             @"http://en.wikipedia.org/wiki/Phlox",@"url",nil]];
67     [blueFlowers addObject:[[NSDictionary alloc]
68                             initWithObjectsAndKeys:@"Pin Cushion Flower",@"name",
69                             @"pincushionflower.png",@"picture",
70                             @"http://en.wikipedia.org/wiki/Scabious",
71                             @"url",nil]];
72     [blueFlowers addObject:[[NSDictionary alloc]
73                             initWithObjectsAndKeys:@"Iris",@"name",
74                             @"iris.png",@"picture",
75                             @"http://en.wikipedia.org/wiki/Iris_(plant)",
76                             @"url",nil]];
77
78     self.flowerData=[[NSArray alloc] initWithObjects:
79                 redFlowers,blueFlowers,nil];
80
81 }

在MASTERVIEW。m中ViewDidLoad方法中调用他

[self  createFlowerData];

实现主视图控制器

 1 - (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
 2     // Return the number of sections.
 3     return [self.flowerSections count];
 4 }
 5
 6
 7 - (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
 8     // Return the number of rows in the section.
 9     return [[self.flowerData objectAtIndex:section] count];
10 }
11
12
13 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   //配置单元格
14 {
15     UITableViewCell *cell = [tableView
16                              dequeueReusableCellWithIdentifier:@"flowerCell"];
17
18     cell.textLabel.text=[[[self.flowerData objectAtIndex:indexPath.section]
19                           objectAtIndex: indexPath.row] objectForKey:@"name"];
20     cell.detailTextLabel.text=[[[self.flowerData objectAtIndex:indexPath.section]
21                                 objectAtIndex: indexPath.row] objectForKey:@"url"];
22
23     UIImage *flowerImage;
24     flowerImage=[UIImage imageNamed:
25                  [[[self.flowerData objectAtIndex:indexPath.section]
26                    objectAtIndex: indexPath.row] objectForKey:@"picture"]];
27
28     cell.imageView.image=flowerImage;
29
30     return cell;
31 }
32
33
34
35
36 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {           //配置每栏标签
37     return [self.flowerSections objectAtIndex:section];
38 }

设置用户选定栏按钮后执行的操作

1 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {      //将Master的属性detailitem设置为选定花朵的NSDictionary,以便详细视图控制器中访问内容
2     self.detailViewController.detailItem=[[flowerData
3                                            objectAtIndex:indexPath.section]
4                                           objectAtIndex: indexPath.row];
5 }

实现详细视图控制器

应将WEB视图加载detailitem中的地址

使用configureView方法(每当视图更新自动调用此方法)

- (void)configureView
{
    // Update the user interface for the detail item.

    if (self.detailItem) {
        NSURL *detailURL;
        detailURL=[[NSURL alloc] initWithString:[self.detailItem objectForKey:@"url"]];
        [self.detailWebView loadRequest:[NSURLRequest requestWithURL:detailURL]];
        self.navigationItem.title = [self.detailItem objectForKey:@"name"];
        self.detailDescriptionLabel.hidden=YES;
    }
}

设置详细视图弹出框

在纵向模式下,分割视图有一个按钮用于显示包含详细视图弹出框,默认为ROOTLIST,我们要加你修改为FLOWER TYPES

1 - (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
2 {
3     barButtonItem.title = NSLocalizedString(@"Flower List", @"Flower List");                 //修改这一行,原先是@Master @Master
4     [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
5     self.masterPopoverController = popoverController;
6 }

为在IPHONE中正常运行

将masterviewcontroller的detailviewcontroller属性设置为该引用

1 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
2     self.detailViewController=segue.destinationViewController;
3 }
时间: 2024-08-02 06:02:07

ios开发(表视图)的相关文章

精通IOS开发-表视图的使用

// // ViewController.m // Simple Table // // Created by Jierism on 16/7/20. // Copyright © 2016年 Jierism. All rights reserved. // #import "ViewController.h" @interface ViewController ()<UITableViewDelegate,UITableViewDataSource> // 声明一个数组,

[转]iOS开发之视图控制器(UIViewController)

视图控制器应该在MVC设计模式中扮演控制层(C)的角色,UIViewController的职责对内管理与之关联的View,对外跟其他UIViewController通信和协调.一个视图控制器管理一个视图(它可以有子视图),其view属性指向它所管理的视图.UIViewController类可以有子类,可以使用一个系统的UIViewController子类或者直接自己创建一个UIViewController的子类. 使用代码创建控制器和视图. 开始创建一个基于窗口的Empty Applicatio

iOS开发系列--视图切换

概述 在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单.在iOS开发中常用的视图切换有三种,今天我们将一一介绍: UITabBarController UINavigationController 模态窗口 UITabBarController iOS三种视图切换的原理各不相同: UITabBarController:以平行的方式管理视图,各个视图之间往往关系并不大,每个加入到UITabBarController的视图都会进行初始化即使当前不显示在

iOS开发中视图控制器ViewControllers之间的数据传递

iOS开发中视图控制器ViewControllers之间的数据传递 这里我们用一个demo来说明ios是如何在视图控制器之间传递重要的参数的.本文先从手写UI来讨论,在下一篇文章中讨论在storyboard中传递数据. 首先新建一个空工程,并添加一个根视图控制器类,如下图所示: # 在函数didFinishLunchingWithOption中添加几行代码,完成后如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 - (BOOL)application:(UIApplication

IOS之表视图单元格删除、移动及插入

1.实现单元格的删除,实现效果如下 Cpp代码   - (void)viewDidLoad { [super viewDidLoad]; //设置导航栏 self.editButtonItem.title = @"编辑"; self.navigation.rightBarButtonItem = self.editButtonItem; [self initTableViewData]; // Do any additional setup after loading the view

IOS开发之视图和视图控制器

视图(View), 视图控制器(ViewController)是IOS开发UI部分比较重要的东西.在学习视图这一块的东西的时候,感觉和Java Swing中的Panel差不多.在UIKit框架中都有一个UIWindow来容纳我们的View.应用程序中几乎全部的可视控件都是UIView以及UIView的子类的实例,并且UIWindow也是UIView的子类.UIWindow可以不借助于父类视图显示在屏幕上,其余的视图都需要添加到父视图中才能显示.窗口是用来显示视图的,下面我们将会结合着实例来具体的

第三章:IOS Table表视图委托协议和数据协议

表视图有两个重要的协议 UITableViewDataSource :数据源协议   方法 返回类型 说明 必须实现 tableView:cellForRowAtIndexPath: UITableViewCell* 为表视图单元格提供数据,该方法是必 须实现的方法 是 tableView:numberOfRowsInSection: NSInteger 返回某个节中的行数 是 tableView:titleForHeaderInSection: NSString 返回节头的标题 否 table

第二章:IOS Table表视图单元格简单介绍

单元格在Table里面也是很总要的一个部分,我们现在就来看看 1. 视图组成结构 图标.标题和副标题可以有选择地设置,扩展 视图可以内置或者自定义,其中内置的扩展视图是在枚举类型UITableViewCellAccessoryType中定义的 UITableViewCellAccessoryType 的常量枚举类型有以下几种: UITableViewCellAccessoryNone.没有扩展图标 UITableViewCellAccessoryDisclosureIndicator.扩展指示器

第四章:IOS Table表视图搜索功能UISearchBar

UISearchBar经常会跟UITable一齐使用,所以在此就介绍一下UISearchBar 先来看看结构 下面再看看它有哪些样式 基本搜索栏.里面????的Search文字用于提示用户??入查询关??字,搜索栏的Placeholder属性可以设置这个提示信息 带有??除按钮的搜索栏.在??入框中??入文字时,会在后面出现??????除按钮,点????除按钮可以??除??入框中的文字 带有查询结果按钮的搜索栏.显示最??搜索结果,显示设定如图4-31所示,选中 Options下的Shows S

IOS之表视图添加搜索栏

下面是我们要实现的效果.本效果是在上一篇自定义表视图的基础上进行更改的.     1.将Search bar and search display拖动到ViewController中.不要添加Search Bar. 2.修改ViewController的头文件 Cpp代码   #import <UIKit/UIKit.h> @interface IkrboyViewController4 : UIViewController { NSArray *dataArr;//用于显示表视图的数据 NS