【问题标题】:iOS two UITableViews on storyboard how to handleiOS 两个 UITableView 在 storyboard 中如何处理
【发布时间】:2012-08-02 04:26:22
【问题描述】:

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

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

我的问题是

我很困惑

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

  • 如何处理单元格的选择?

请帮忙!

【问题讨论】:

  • 是的,您可以为不同的表视图添加不同的数据源,只需为每个表视图定义 IBOutlate,据我所知,您可以将表视图与它们的 tab 属性分开,这将起作用
  • 你能告诉我如何为每个表视图定义 IBOutlate。设置 IBOutlate 后,是否应该创建表视图的实例并将其添加到视图控制器上?
  • 看看下面的答案就可以看出我在上面的评论中所说的

标签: uitableview xcode4.2 storyboard ios5


【解决方案1】:

首先:为什么不直接在占据整个屏幕的推送视图控制器上显示文件?对我来说似乎更直观。

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

像这样指定两个 tableView 属性:

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

实现 UITableVIew 的两个委托协议

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> 

2、 将两个 UITableView 添加到您的 ViewController 中,然后...

  • ...将您的 Outlets 链接到两个表格视图
  • ...在属性检查器上,将foldersTableView 的Tag 设置为1,将filesTableView 的Tag 设置为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;
}
}

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 之前的版本,请不要忘记 @synthesize .m 文件中的属性。

【讨论】:

  • 应该是 (tableview.tag == 1) 而不是 (tableview.tag = 1)
【解决方案2】:

如果你使用多个表,别忘了酌情隐藏:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    相关资源
    最近更新 更多