iOS故事板上的两个UITableViews如何处理

sai*_*esh 2 storyboard uitableview ios5 xcode4.2

在我的iPad应用程序故事板设计中,添加了两个表视图.

一个表视图用于显示文件夹名称,另一个表视图用于显示文件名.当选择文件夹表格视图单元格时,所选文件夹内的文件需要显示在另一个表格视图(文件表格视图)中.

我的问题是

我很困惑

  • 如何为每个表视图添加委托和数据源ViewController?或者是否可以在ViewController以外的自定义类中为每个表视图添加数据源和委托?

  • 如何处理细胞的选择?

请帮忙 !

Zol*_*tók 5

首先:你为什么不在推出的viewController上显示文件,它占用了整个屏幕?对我来说似乎更直观.

如果你想用两个tableViews做,并假设他们使用动态单元格:
1,视图控制器.h文件中

指定两个tableView属性,如下所示:

@property (weak, nonatomic) IBOutlet UITableView *foldersTableView;
@property (weak, nonatomic) IBOutlet UITableView *filesTableView;
Run Code Online (Sandbox Code Playgroud)

实现UITableVIew的两个委托协议

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> 
Run Code Online (Sandbox Code Playgroud)

2,将两个UITableView添加到ViewController上,然后......

  • ...将您的奥特莱斯链接到两个表格视图
  • ...在属性检查器上将foldersTableView的Tag设置为1,将filesTableView设置为2
  • ...选择每个UITableViews,转到Connections Inspector并将它们的两个委托方法(委托和数据源)链接到ViewController(对于它们两者)

3,在ViewController的.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;
}
}
Run Code Online (Sandbox Code Playgroud)

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
    }
}
Run Code Online (Sandbox Code Playgroud)

希望我能正确地得到一切.@synthesize如果您使用的是pre XCode 4.4,请不要忘记.m文件中的属性.