【问题标题】:Loading multiple tableviews in a UIView (UIView Controller)在 UIView(UIView 控制器)中加载多个表视图
【发布时间】:2012-09-10 02:13:11
【问题描述】:

我一直在整个互联网上寻找帮助,但是我手头的问题几乎没有解决方案。我试图喘口气的项目有点独特(用户界面并不完全遵循典型规范)。

目前的发展环境:

  • xcode 4
  • 故事板代替笔尖

下面是我想要完成的图表——全部在 UIView 控制器中:

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

我确信有经验的开发人员完全了解我的问题,或者我要问什么。我知道静态 UITableView 需要在 tableview 控制器中,但我需要同时显示两个 UItableView,这意味着它必须在 UIView 中。

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

所以基本上,我是在询问方向......但代码示例也不会受到伤害!谢谢你 100x 结束了!

-乔纳森

【问题讨论】:

  • 这是适用于 iPhone 还是 iPad?这有很大的不同。
  • 你在使用 UITableViewController 吗?还是带有 UITableView+Delegate+Datasource 方法的 UIViewController?
  • 这是给 iPad 的,很抱歉造成混乱。 “默认”视图是 UIViewController。

标签: ios xcode uitableview ipad


【解决方案1】:

UITableViewController只是一个专门的UIViewController,专门设计用于显示全屏UITableViews。 (相当)等价于使用UITableViewController 子类或UIViewController <UITableViewDataSource, UITableViewDelegate> 子类来管理表格视图。

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

这甚至是一种管理不全屏的 tableview 的方法(因为 UITableViewController 期望它的 view 属性直接是它管理的 UITableView,而不是它的主视图的子视图或其他任何东西,并且因此期望UITableView 占据整个屏幕,这与使用具有UITableViewUIViewController 作为其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;
}

【讨论】:

  • 你确定吗? if (tableView == self.masterTableView) ?? else if (tableView = self.detailsTableView) ??
  • 是的,为什么?我只是将传递给数据源方法的tableView 参数与我的两个表视图中的每一个进行比较,以查看我需要为这两个中的哪一个构建一个单元格。这里有什么问题? (请注意,您不需要在这里使用isEqual: 比较这些,因为在这种情况下指针比较就足够了)
  • 太好了,今天我回家后,我将开始重建我的课程。我一定会回到这里让大家知道进展如何!
  • 这是一个很好的开始。在为我的新字段等添加 plist 支持后,我的代码基本上翻了一番,但你让我开始运行!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-18
  • 1970-01-01
  • 2018-04-19
  • 1970-01-01
相关资源
最近更新 更多