2012-08-04 20 views
2

在我的iPad應用程序故事板設計中,添加了兩個表格視圖。iOS的兩個故事板上的UITableViews如何處理

一個表視圖是顯示文件夾名稱和其他表視圖是顯示文件名。如果選擇了文件夾表格視圖單元格,則所選文件夾內的文件需要顯示在另一個表格視圖(文件表格視圖)中。

我的問題是

我感到困惑

  • 如何添加委託和數據源中的每個ViewController表視圖?或者是否有可能爲ViewController以外的自定義類中的每個表視圖添加數據源和委託?

  • 如何處理小區的選擇?

請幫忙!

+1

是的,你可以添加不同的數據源不同tableViews只定義了'IBOutlate'每個表視圖,你可以在分叉tabelViews與他們的'tab'屬性根據我的知識,這將工作 – 2012-08-04 09:33:36

+0

你能告訴我如何爲每個表視圖定義IBOutlate。在設置了IBOutlate之後,我應該將表視圖的一個實例創建並添加到視圖控制器中? – saikamesh 2012-08-04 10:27:59

+0

只是看到下面的答案,這顯示了我在上面評論中說過的內容 – 2012-08-06 05:13:07

回答

5

首先:爲什麼你不只是顯示在推動viewController佔用整個屏幕上的文件?對我來說似乎更直觀。

如果你想有兩個tableViews做到這一點,並且假設他們使用動態細胞比:
1,在您的視圖控制器的.h文件中

指定一個像於是兩個的tableView屬性:

@property (weak, nonatomic) IBOutlet UITableView *foldersTableView; 
@property (weak, nonatomic) IBOutlet UITableView *filesTableView; 

實施的UITableView的兩個委託協議

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> 

2,兩個UITableViews添加到您的視圖控制器,並...

  • ...您的奧特萊斯鏈接到兩個tableviews
  • ...在屬性檢查器中設置的標籤在foldersTableView爲1,filesTableView 2
  • ...選擇每個UITableViews的,轉到連接督察和他們的兩個委託方法(委託和數據源)鏈接到你的ViewController(對他們倆的)

3,在。您的視圖控制器的.m文件實施的UITableView的三個數據源的方法,像這樣:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    if (tableView.tag == 1) { 
     return theNumberOfSectionsYouWantForTheFolderTableView; 
    } else { 
     return theNumberOfSectionsYouWantForTheFilesTableView; 
    } 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    if (tableView.tag == 1) { 
     return [foldersArray count]; 
    } else { 
     return [filesArray count]; 
    } 
} 

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

    // Configure the cell... 
    return cell; 

} else { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    // Configure the cell... 

    return cell; 
} 
} 

4,落實的選擇:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (tableView.tag == 1) { 
     //fill filesArray with the objects you want to display in the filesTableView... 
     [filesTableView reloaddata]; 
    } else { 
     //do something with the selected file 
    } 
} 

希望我正確地得到的一切。如果您使用的是前XCode 4.4,請不要忘記.m文件中的屬性@synthesize

+0

+1解釋我在上面提到的東西 – 2012-08-06 05:13:33

+1

它應該是(tableview.tag == 1)not(tableview.tag = 1) – jcrowson 2013-07-08 10:41:05

+0

Thanks,fixed它! – 2013-07-08 12:10:19

0

如果您正在使用多個表,不要忘記隱藏酌情:

- (void)viewWillAppear:(BOOL)animated 
{ 
    [self.table2 setHidden:YES]; 
    [self.table1 setHidden:NO]; 
    // Probably reverse these in didSelectRow 
}