一个view两个tableview

http://www.trappers.tk/site/2011/06/16/mainwindow-xib/

1,首先是数组赋值时注意最后一个nil不能少。

     NSArray * array =[ [NSArray alloc]initWithObjects:@"m1",@"m2",@"m3",nil];

2.界面启动不起来的话看viewDidLoad方法

 

3. 如果是2个表格则创建两个TableView对象,然后在IB中连接上去。然后再在numberOfRowsInSection方法和cellForRowAtIndexPath里判断是哪个表格对象

     if(tableView == self.tab1)

       return [self.listData count];

   else if(tableView == self.tab2)

     return [ self.listData count];

 

iphone tableview 一些常用方法

今天在写iphone与表格视图相关的功能,顺便记几个方法,为以后查看使用
UITableViewController 实现数据源 UITableViewDataSource和委托UITableViewDelegate之后,一些常用方法,
1.  UITableViewDataSource必须实现的方法
 返回表格行数的方法 ( UITableViewDataSource)
-(NSInteger)tableView:(UITableView*) tableView numberOfRowsInSection:(NSInteger)section
此方法返回一个整形数,指示表格的行数

返回表格单元格的方法

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


2. UITableViewDelegate方法

 

控制表格哪些行可以选中的方法

-(NSIndexPath *)tableView:(UITableView *)tableView

willSelectRowAtIndexPath:(NSIndexPath*)indexPath

 

 

/


创建简单表视图


主要介绍表示图的建立,数据源和委托等一些简单的方法。

 

1.新建Empty Application,命名SimpleTable

2.新建UIviewController subclass,取名SimpleTableViewController,并选中创建xib文件。

3.接下来在SimpleTableViewController.xib中布局,拖动一个TableView到Objects中,使其成为View的子视图。很明显,这里就是我们要构建子视图的地方啦。如何把这个子视图添加到window中去呢?我们看到在SimpleTableAppDelegate中已经建立了一个table,现在我们需要在此添加一个输出口来实例化我们在interface builder总布置的表视图。

//  SimpleTableAppDelegate.h

 

#import <UIKit/UIKit.h>

@class SimpleTableViewController;

@interface SimpleTableAppDelegate : UIResponder <UIApplicationDelegate>{

    IBOutlet SimpleTableViewController *viewController;

}

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) IBOutlet SimpleTableViewController *viewContrller;

@end

 

 

//  SimpleTableAppDelegate.m

#import "SimpleTableAppDelegate.h"

#import "SimpleTableViewController.h"//导入所需类的头文件

@implementation SimpleTableAppDelegate

 

@synthesize window = _window;

@synthesize viewContrller = _viewContrller;//settergetter,养成在这里加下划线的好习惯吧

 

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

{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    SimpleTableViewController *simpleTableviewContrller=[[SimpleTableViewControlleralloc]initWithNibName:@"SimpleTableViewController" bundle:nil];//实例视图

    _viewContrller=simpleTableviewContrller;

    self.window.backgroundColor = [UIColor whiteColor];

    self.window.rootViewController=_viewContrller;//把刚才实例的SimpleTableViewController作为根视图

    [self.window makeKeyAndVisible];

    return YES;

}

……

现在你可以使者运行一下,应该可以看到一个空的表啦。(中间用横线分割了很多段)

[转载]xcode4.2 <wbr>iOS <wbr>5 <wbr>创建简单表视图

3.这里的表是空的,那如何给表添加内容呢?

其实表视图本身就包含了委托和数据源两个协议:UITableViewDelegate, UITableViewDataSource

 

那我们现在就把这两个协议写给SimpleTableViewController吧。

//  SimpleTableViewController.h

#import <UIKit/UIKit.h>

 

@interface SimpleTableViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>{

    NSArray *listData;

}

@property (strong, nonatomic) NSArray *listData;

 

@end

我们这里使用数组作为表示图的数据源。

 

//  Simple_TableViewController.m

#import "Simple_TableViewController.h"

 

@implementation Simple_TableViewController

@synthesize listData;

 

……

 

//首先实例一个数组作为数据源使用,它应该在视图加载完毕后执行

- (void)viewDidLoad

{

    NSArray *array=[[NSArray alloc] initWithObjects:@"Sleepy", @"Sneezy", @"Bashful", @"Happy", @"Grumpy",@"Dopey", @"Thorin", @"Dorin", @"Nori", @"Ori", @"Balin", @"Dwalin", @"Fili", @"Kili", @"Oin", @"Gloin", @"Bofur",@"Bombur", nil];

    self.listData=array;

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

 

表视图从其结构来看,应该遵循表-分区-行这样的结构。这里我们能只创建一个分区,所以不用调用分区的方法(如果多个分区,那可就要调用啦),但是行是一定要说明的。

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

    return [self.listData count];//返回一个NsInterger作为行数

}

现在我们已经知道了行数就是我们刚才建立的那个数组的长度。但是我们怎么告诉每一行显示什么内容呢?那就要用到以下的一个方法了,大致的模式如下:

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

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

    if(cell==nil){

        cell =  [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:SimpleTableIdentifier];

    }

现在已经有行单元了,我们需要对这个行单元的一些属性进行设置,如图片,文本等,最后我们

return cell;

即可。

 

以我们现在的例子来看,源代码如下:

 

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

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

    if(cell==nil){

        cell =  [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:SimpleTableIdentifier];

    }

    

    NSUInteger row = [indexPath row];

    cell.textLabel.text = [listData objectAtIndex:row];

    return cell;

}

以上这两个方法都是TableViewDataSource协议里的方法。

4.好啦,到这里,你是不是觉得已经完成了呢?那就运行一遍。还是空表,怎么回事呢?嗯,我们定义了表的数据源,但是表视图并不知道我们在哪里定义了啊。所以,我们要告诉表示图其dataSource(即以上两个方法(当然如果还有其他的一些方法))在哪里定义了。这个例子中我们是写在SimpleTableViewController中的,所以,理所当然,我们在SimpleTableViewController.xib中需要把表示图连接到File’s Owner:

具体的操作是按住ctrl同时拖动Table View到File’s Owner两次,分别连接到dataSource和delegate。

现在运行一遍,嗯,可以啦。

[转载]xcode4.2 <wbr>iOS <wbr>5 <wbr>创建简单表视图


5.下面继续介绍TableViewDelegate协议里的方法

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    return indexPath;

}

@end

这里这个方法是在你点击某个视图单元时表示图用来判断你是否你选中你点击的表图单元。上面这一段你即使不写都可以的,因为默认就是每个表图单元都可以选中的。但是如果你要使某个单元选不中,那就要加点料啦。例如这样:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSUInteger row = indexPath.row;

    if(row == 0)

        return nil;

    return indexPath;

}

@end

第一个行就选不到咯。

 

 

单击某个表视图单元后应该执行一些操作吧,那这些操作该写到哪里呢?那就是接下来这个方法了:

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

    

}

例如,弹出一个警告

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

    NSUInteger row = indexPath.row;

    NSString *rowValue = [listData objectAtIndex:row];

    

    NSString *msg = [[NSString alloc]initWithFormat:@"You selected %@", rowValue];

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Row Slected" message:msg delegate:nilcancelButtonTitle:@"Yep, I did" otherButtonTitles: nil];

    [alert show];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];//在弹出警告后自动取消选中表视图单元

}

 

[转载]xcode4.2 <wbr>iOS <wbr>5 <wbr>创建简单表视图

在运行一遍,很炫吧,试一下点第一行,你点我不到,哈哈。

 

实现输入键盘能够点击任何画面任何一个地方都能关闭

 

1)在viewcontroller.h添加一个操作

 - (IBAction)backgroundTap:(id)sender;

2)在viewcontroller.m实现改操作

  -(IBAction)backgroundTap:(id)sender

{

     [textfiled   resignFirstResponder];

}

3)在IB中,选择view对象按苹果键+4,将其更改为UIController响应各项事件

4)从苹果键+2中选择Touch down事件到File's  owner图标,选择backgoundTap操作。

 

 

 

 

///

XCode 4.2开发笔记图文(3)---多视图开发的初步了解
2011/12/02 14:23

今天看了一下iOS开发中多个视图的应用.

iOS下包含了几种不同种类的视图类型和controller:比如Tab Bar ,Navigation Bar ,Tool Bar等.也可以自定义自己的视图的controller

程序中主窗口的视图控制器我们成为root controller,由它负责不同视图的切换等功能.

由root controller负责的视图都有自己的controller和delegate,比如一个tab bar,当用户在tab bar上点击的时候,是由tab bar的controller负责处理,而当用户在内容界面点击的时候,是由内容视图的controller负责处理的.

书中的例子很简单,点击tab bar中的按扭,在两个背景颜色(一蓝一黄)的视图中切换,两个视图中各有一个button

书中的例子建立的步骤如下:

1.建立一个Window base application ,没有view controller,只有一个window

2.添加视图文件

2.1 新建文件,选择cocoa touch下的UIViewController subclass 不使用xib文件(书中这里是xcode4.2以前的版本) 保存为SwitchViewController.h 和 SwitchViewController.m.这就是root controller

2.2 按照步骤2.1,创建蓝,黄背景视图的类文件 BlueViewController & YellowViewController

3.添加xib文件

3.1 选择cocoa touch下user interface下的View XIB的文件

3.2 新建两个视图的xib文件,BlueView.xib & YellowView.xib

4.在AppDelegate中添加一个IBOutlet,

@property (nonatomic, retain) IBOutlet SwitchViewController   *switchViewController;

5.为了将主视图SwitchViewController和Window关联,需要使用addSubview,添加了以下代码:

#import "View_SwitcherAppDelegate.h" 
#import "SwitchViewController.h" 
@implementation View_SwitcherAppDelegate 
@synthesize window; 
@synthesize switchViewController; 
- (void) application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ 
    // Override point for customization after application launch 
   [self.window addSubview:switchViewController.view]; 
    [self.window makeKeyAndVisible]; 
    return YES; //注:书中代码这样写的,返回YES 但是函数类型是void.经查SDK,发现函数类型是BOOL

- (void)dealloc { 
    [window release]; 
   [switchViewController release]; 
    [super dealloc]; 
}

6. 为了切换两个view,我们在SwitchViewController里添加两个View的指针,不定义IBOutlet,同时定义方法switchViews

#import <UIKit/UIKit.h> 
@class YellowViewController; 
@class BlueViewController; 
@interface SwitchViewController : UIViewController { 

@property (retain, nonatomic) YellowViewController *yellowViewController; 
@property (retain, nonatomic) BlueViewController *blueViewController; 
- (IBAction)switchViews:(id)sender; 
@end

 

7.代码架构好了,开始在IB中操作

7.1 在Library中拖动一个View Controller到Window上面

7.2 该View是UIViewController,在Identity Inspector中修改类名为UIViewController

7.3 新建Toolbar的View,拖放一个View到7.1添加的View上面,替换原有的View(注:为何是替换呢?)

7.4 拖动一个Toolbar到7.3新建的View中,选中Toolbar后,点击Button,链接到SwitchViewController的方法switchView中

 

8. 在IB中将AppDelegate中的IBOutlet switchViewController与类SwitchViewController链接

9. 修改SwitchViewController.m

主要就是从Nib加载ViewController,不用的释放Controller

刚开始只加载BlueView,因为YellowView可能用户不会选择,等到切换的时候才加载(Lazy Loading)

#import "SwitchViewController.h" 
#import "YellowViewController.h" 
#import "BlueViewController.h" 
@implementation SwitchViewController 
@synthesize yellowViewController; 
@synthesize blueViewController; 
- (void)viewDidLoad 

    BlueViewController *blueController = [[BlueViewController alloc] 
              initWithNibName:@"BlueView" bundle:nil]; 
    self.blueViewController = blueController; 
   [self.view insertSubview:blueController.view atIndex:0]; 
    [blueController release]; 
    [super viewDidLoad]; 

- (IBAction)switchViews:(id)sender 

    if (self.yellowViewController.view.superview == nil) 
   { 
        if (self.yellowViewController == nil) 
        { 
            YellowViewController *yellowController = 
           [[YellowViewController alloc] initWithNibName:@"YellowView" 
                                                   bundle:nil]; 
            self.yellowViewController = yellowController;

            [yellowController release]; 
        } 
        [blueViewController.view removeFromSuperview]; 
        [self.view insertSubview:yellowViewController.view atIndex:0]; 
   } 
   else 
   { 
      if (self.blueViewController == nil) 
      { 
          BlueViewController *blueController = 
          [[BlueViewController alloc] initWithNibName:@"BlueView" 
                                               bundle:nil]; 
          self.blueViewController = blueController; 
          [blueController release]; 
      } 
      [yellowViewController.view removeFromSuperview]; 
      [self.view insertSubview:blueViewController.view atIndex:0]; 
   } 
}

Releases the view if it doesn't have a superview 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview 
    [super didReceiveMemoryWarning]; 
    // Release any cached data, images, etc, that aren't in use 
    if (self.blueViewController.view.superview == nil) 
        self.blueViewController = nil; 
    else 
       self.yellowViewController = nil; 
}

 

- (void)dealloc { 
    [yellowViewController release]; 
    [blueViewController release]; 
    [super dealloc]; 

@end

 

10.完善两个内容视图,添加button,弹出不同的内容提示.

#import <UIKit/UIKit.h> 
@interface BlueViewController : UIViewController { 

- (IBAction)blueButtonPressed; 
@end

在实现中加入UIAlertView即可.(略去)

 

打开BlueView.nib,在Indentity Inspector中选择class name为BlueViewController,表明从nib从BlueViewController

然后修改View的背景色,更改View的位置在属性页的

Simulated User Interface Elements选项下

 

11.加入视图切换动画

- (IBAction)switchViews:(id)sender 

    [UIView beginAnimations:@"View Flip" context:nil]; 
    [UIView setAnimationDuration:1.25]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

.....

 

if(..)

{

        [UIView setAnimationTransition: 
         UIViewAnimationTransitionFlipFromRight 
                               forView:self.view cache:YES]; 
        [blueViewController viewWillAppear:YES]; 
        [yellowViewController viewWillDisappear:YES];

        … …

}

[UIView commitAnimations];

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

昨天参照书中的例子,回到家后开始实现示例中的代码.

发现XCode4.2中没有Window Base Application这一选项.使用Empty Application没有主界面的Storyboard文件.

于是打开google,发现Jeroen Trappers有一篇文章有详细的解决方法.

地址:http://www.trappers.tk/site/2011/06/16/mainwindow-xib/

 

我的看法是(不一定准确):AppDelegate是个代理类,起类似与window编程下的WNDPROC函数.

与其配合的是UIApplication.

在没有XIB文件的情况下,是通过程序的方式加载UIApplication的一个实例完成框架的建立的.

为了从XIB文件中加载,我们就要自己创建一个XIB文件,这是首先的问题.

如何将XIB文件和AppDelegate的类关联是下面的问题.

 

我们知道,AppDelegate需要一个UIApplition实例,这个事例保存所有XIB上元素的拷贝.所以File's Owner的类名我们要改为UIApplicaion

同时UIApplicaion有一个插座IBOutlet刚好是我们AppDelegate可以对接的.类型UIApplicationDelegate

于是我们首先拖放一个Object,修改类名为我们的xAppDelegate,这样就可以将这个Object和UIApplication中的delegate链接了.

delegate

The delegate of the application object.

@property(nonatomic, assign) id<UIApplicationDelegate> delegate

Discussion

The delegate must adopt the UIApplicationDelegate formal protocol. UIApplication assigns and does not retain the delegate.

Availability
  • Available in iOS 2.0 and later.
Declared In
UIApplication.h

 

源文件中有UIWindow的变量 window ,我们需要在上面添加的Object(类名xAppDelegate)里面创建一个Window,然后把变量加上IBOutlet后进行链接.

这样就完成了XIB中AppDelegate和它下面Window的两个对象从XIB到代码之间的链接.

 

可是此时程序的入口并没有改变,文章中推荐的方式是在工程配置中的Main Interface修改为一个XIB文件名,其实就是在程序中加载XIB文件作为入口.

同样的功能,也许我们在代码中也可以实现.类似与initWithNibName的方法,暂时我没有实现.

 

还有就是注释掉了一个初始化的函数:- (BOOL) application:didFinishLaunchingWithOptions: 这个方法中的操作是跟我们从XIB加载相冲突的.

 

通过以上几个步骤,我们就可以从Empty Application中自定义的加载一个XIB文件了.

学习到这里,让我对整个XIB的工作方式和UIApplication的工作原理又有了更深一步的了解.

对于以后的多视图学习非常的有帮助.

 

以上操作的详细步骤如下,顺便复习一下:

1.创建Empty application,此时项目中只有一个AppDelegate的类

2.新建文件,选择User Interface下的Empty 命名为MainWindow

3.打开新建的MainWindow.xib文件

 

 

 

4.将File's Owner的类名修改为UIApplication

5. 在Library中拖放一个Object到图下的位置

6.将该object的类名修改为文件中AppDelegate的类名(同时还可以给object命名下Label)

7.拖放一个Window到左边

8. 在xAppDelegate的h文件中,给window的属性加上IBOutlet

@interface DemoAppDelegate :      UIResponder <UIApplicationDelegate>@property (strong, nonatomic) IBOutlet UIWindow *window;@end

 

 

9.将File's Owner 和 拖放的Object按照以下方式连接

 

 

 

10.在项目属性中,把Main Interface修改成你的xib文件名MainWindow

11. 在xAppDelegate.m文件中,将

- (BOOL) application:didFinishLaunchingWithOptions:

 

这个方法全部注释掉

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值