ios - 在 UIView 中加载多个 tableview(UIView Controller )

标签 ios xcode uitableview ipad

我一直在整个互联网上寻找帮助,但是对于我手头的问题几乎没有解决方案。我想惊叹的项目有点独特(UI 并不完全遵循典型规范)。

当前的开发环境:

  • Xcode 4
  • 用 Storyboard代替 Nib

下面是我想要完成的任务的图表 - 全部在 UIView Controller 中:

enter image description here

  • UIView是浅灰色背景
  • UITableView 1 - 这是一个静态的(或者它可以是动态的,这是另一个挑战)UITableview,它将保存不同的数字 计算值
  • UITableView 2 - 这是一个 UITableview,每次运行时都会保存计算结果。
  • UIImageView 1 - 这是一个计算图像示例(我已经弄清楚了)

我确信经验丰富的开发人员完全了解我的问题,或者我要问的问题。我知道静态 UITableView 需要位于 tableview Controller 中,但我需要同时显示两个 UItableView,这意味着它必须位于 UIView 中。

我可以通过 IB 使界面看起来像我需要的那样,但是在尝试编译和构建时,我收到错误,要求 UITableView 位于 UITableViewController 而不是 UIView Controller 中。我见过很多使用主从布局的例子,但唯一的规定是这个 UITableview 需要在这个 View 中 100% 的时间显示。

所以基本上,我是在寻求方向......但代码示例也没有什么坏处!谢谢你 100 倍了!

-乔纳森

最佳答案

UITableViewController只是一个专门的UIViewController专为全屏显示而设计UITableView s。它(非常)相当于使用 UITableViewController子类或 UIViewController <UITableViewDataSource, UITableViewDelegate>管理桌面 View 的子类。

所以即使 UITableViewController有一些更特殊的行为(如果不存在则自动创建 UITableView,自动滚动它以显示键盘,将其自身设置为它管理的唯一 delegatedataSourceUITableView 等),您可以使用标准UIViewController管理UITableView并成为其dataSource来填充它。

这甚至是管理不全屏的表格 View 的一种方法(因为 UITableViewController 期望其 view 属性直接是它管理的 UITableView ,而不是其主视图或其他 View 的 subview ,并且因此,期望 UITableView 占据整个屏幕,这与使用 UIViewController 相反,后者将 UITableView 作为其 view 的自定义大小子类)


因此,就您而言,您可以拥有 UIViewController有两个 IBOutlets ,各一个tableView ,以及独特的UIViewController可以是dataSource (和 delegate )两者 UITableViews 。那不是问题。请在数据源方法中小心区分是返回第一个还是第二个 UITableView 的数据。每次都提供正确的表格。

@interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) IBOutlet UITableView* masterTableView;
@property (nonatomic, retain) IBOutlet UITableView* detailsTableView;
@end

@implementation MyViewController
@synthesize masterTableView = _masterTableView;
@synthesize detailsTableView = _detailsTableView;

// Proper memory mgmt not shown here:
//  - don't forget to set self.masterTableView and self.detailsTableView to nil in viewDidUnload
// - and to release _masterTableView and _detailsTableView in your dealloc method

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    UITableViewCell* cell;
    if (tableView == self.masterTableView)
    {
        static NSString* kMasterCellIdentifier = @"MasterCell";
        cell = [tableView dequeueReusableCellWithIdentifier:kMasterCellIdentifier];
        if (!cell)
        {
          cell = [[[UITableViewCell alloc] initWithReuseIdentiier:kMasterCellidentifier] autorelease];
          // do some configuration common to all your master cells
        }
        // configure the rest of your cell for each property that is different from one cell to another
    }
    else if (tableView == self.detailsTableView)
    {
        // Do exactly the same principle, but for the cells of your "details" TableView
    }
    return cell;
}

关于ios - 在 UIView 中加载多个 tableview(UIView Controller ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12466428/

相关文章:

ios - 在没有 segue 的 2x ViewController 之间传递信息时,Delegate 始终为 nil

objective-c - 如何使用 Objective C 查找和替换文件中的文本?

ios - 如何使用 Swift 3 iOS 应用程序读取 plist

ios - 将选定行的值传递给 View Controller

Android实现从左向右滑动动画

iOS 使用日期选择器动态更改单元格的内容

iOS:UITableViewCell - 单元格相互中断/重叠

objective-c - Xcode 4.4 - 无法添加编译器标志来编译源

iOS UITableView : assign background color as gradient using CAGradientLayer

ios - 需要添加一个 View 作为 TableHeader?