2012-07-08 61 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;