2012-07-08 67 views
-1

的按I制成的按钮,它连接到在界面生成器的动作。在操作方法中,从显示视图切换到显示表视图需要做什么?切换到的UITableView上的按钮

下面是我的一些代码:

// SwitchToTableViewController.h 

#import <UIKit/UIKit.h> 

@interface SwitchToTableViewController : UIViewController { 


} 

-(IBAction) switch : (id) sender; 

@end 

//SwitchToTableViewController.m 
#import "SwitchToTableViewController.h" 
#import "Table.h" 
@implementation SwitchToTableViewController 

-(IBAction) switch : (id) sender{ 

    // what is i need to write here to switch to tableview 


} 

@end 

//table.m 
#import <UIKit/UIKit.h> 


@interface Table : UITableViewController { 


} 

@end 

//table.h 
#import "Table.h" 


@implementation Table 

#pragma mark Table view methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 


// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 2; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Set up the cell... 

    return cell; 
} 

@end 
+0

你在这里有大量的代码是完全不相关的。它使人们无需阅读这个问题 - 不需要在项目中包含每行代码。 – jrturton 2012-07-08 11:58:26

回答

0

您需要:

  1. 立即创建表格。

  2. 传递任何数据到这个瞬间,最有可能的一个数组,这样就可以用它来显示单元中的数据。您需要先在Table类中创建一些iVar。目前你没有任何。

  3. 选择了多种方式来呈现你的表瞬间之一。

看着苹果的 doc。它有很多示例代码。

编辑示例代码:

在你SwitchToTableViewController.h文件,加入这一行:

#import "Table.h" 

在你SwitchToTableViewController.m,使这改变

-(IBAction) switch : (id) sender{ 

    // what is i need to write here to switch to tableview 
    Table *myTable = [[Table alloc]init]; 
    myTable.cellArray = [NSArray arrayWithObjects:@"Item1", @"Item2", @"Item3", nil]; //only an example to be displayed 
    [self presentViewController:myTable animated:YES completion:nil]; // present your Table - view controller 

} 

Table.h文件应该是这样的:

#import <UIKit/UIKit.h> 

@interface Table : UITableViewController { 

} 
@property (nonatomic, strong) NSArray *cellArray; // adding this line to get data from the parent 
@end 

在你Table.m,使这改变

@implementation Table 

@synthesize cellArray; //add this line right after @implementation Table 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

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

    // Return the number of rows in the section. 
    return cellArray.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    // Configure the cell... 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.textLabel.text = [cellArray objectAtIndex:indexPath.row]; 
    } 

    return cell; 
} 
0

你有两种观点控制器这里,不是两个视图。认识这两个概念之间的差异非常重要。我在下面的答案中用粗体显示了一些你需要阅读的关键词。

要显示响应就像一个按钮,按下另一个视图控制器,您需要创建您的目的地视图控制器的新实例,然后把它在屏幕上莫名其妙。

有很多方法可以做到这一点,但对于初学者来说,最简单的方法是在故事板中执行所有操作。将两个视图控制器添加到故事板。

您的初始视图控制器(带按钮)应该嵌入到UINavigationController中。从按钮控制 - 将拖动到您的表格视图控制器。这将创建一个赛格。从选项列表中选择“推送”。

嘿presto,当您按下按钮时,您的桌面视图现在将出现在屏幕上。

0

首先创建一个SingleView应用程序(基于UIViewController)将其命名为SwitchingView。

然后在视图上方添加一个UITableView。使用界面构建器连接。将TableView的委托和数据源设置为SwitchingView。

在视图上添加一个按钮(在添加表视图之前)。设置表视图是使用IB(界面构建器)隐藏的。

在按钮动作做

 tableview.hidden = NO; 

这将显示你的表视图!如果您想再次显示视图,只需在表格视图:didSelectForIndexPathRow方法中制作

 tableview.hidden = YES;