在UIView(UIView控制器)中加载多个tableviews [英] Loading multiple tableviews in a UIView (UIView Controller)

查看:68
本文介绍了在UIView(UIView控制器)中加载多个tableviews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在互联网上寻求帮助,但是我手头的问题几乎没有解决方案。我试图获得喘息的项目有点独特(UI并不完全符合典型规范)。



当前发展环境:




  • xcode 4

  • 故事板而不是笔尖



下面是我想要完成的图表 - 所有这些都在UIView控制器中:






  • UIView是浅灰色背景

  • UITableView 1 - 这是一个静态的(或者它可以是动态的,这是另一个挑战)UITableview将保存不同的数字
    值进行计算

  • UITableView 2 - 这是一个UITableview,它将在每次运行时保存计算结果。

  • UIImageView 1 - 这是一个计算的图像示例(我已经想到了)



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



<我可以通过IB使界面看起来像我需要它,但是当我尝试编译和构建时,我收到的错误要求UITableView在UITableViewController中,而不是UIView控制器。我看过许多使用主 - 细节布局的例子,但唯一的规定是这个UITableview需要在这个视图中100%显示。



所以基本上,我要求方向......但代码示例从来没有伤害过!谢谢100x结束了!



-Jonathan

解决方案

UITableViewController 只是一个专门用于显示全屏 UITableView 的专用 UIViewController 。它(相当)等同于使用 UITableViewController 子类或 UIViewController< UITableViewDataSource,UITableViewDelegate> 子类来管理tableview。



所以即使 UITableViewController 还有更多的spiecialized行为(如果它不存在,自动创建UITableView,自动滚动显示键盘,将其自身设置为唯一委托 dataSource UITableView 它管理等),你可以使用标准的 UIViewController 来管理 UITableView 并将其作为 dataSource 来填充它。



这甚至可以管理一个没有充分利用的表视图屏幕(如 UITableViewController 期望其视图属性直接为 UITableView 它管理,而不是其主视图的子视图或其他任何东西,因此需要 UITableView 整个屏幕,与使用 UIViewController 相比,它具有 UITableView 作为其<$ c的自定义大小的子类$ c>查看)






所以在你的情况下,你可以有一个 UIViewController 有两个 IBOutlets ,每个 tableView 一个,并且唯一 UIViewController 可以是 dataSource (以及委托 both UITableViews 。那不是问题。请小心,然后在数据源方法中区分是否要返回第一个或第二个 UITableView 的数据,以便每次都提供正确的表。

  @interface MyViewController:UIViewController< UITableViewDataSource,UITableViewDelegate> 
@property(非原子,保留)IBOutlet UITableView * masterTableView;
@property(非原子,保留)IBOutlet UITableView * detailsTableView;
@end

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

//这里没有显示正确的内存管理:
// - 不要忘记在viewDidUnload
//中将self.masterTableView和self.detailsTableView设置为nil - 并且在你的dealloc方法中释放_masterTableView和_detailsTableView

- (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];
//对所有主单元执行一些通用配置
}
//为每个不同的单元格配置其余单元格
}
else if(tableView == self.detailsTableView)
{
//完全相同的原则,但对于你的细节的单元格TableView
}
返回单元格;
}


I've been searching all throughout the internet for assistance, however there has been little to no solutions to my issue at hand. My project that im trying to get a gasp on is somewhat unique (UI is not exactly following the typical norms).

Current Development Enviroment:

  • xcode 4
  • storyboards instead of nibs

Below is a diagram of what i am trying to accomplish -- all within a UIView Controller:

  • UIView is the light grey background
  • UITableView 1 - this is a static (or it can be dynamic, thats another challenge) UITableview which will hold different numeric values for calculation
  • UITableView 2 - this is a UITableview which will hold calculated results every time it is run.
  • UIImageView 1 - this is a calculated image example (I have that figured out)

Im sure experienced developers are fully aware of my issue, and or what im about to ask. I understand that a static UITableView is required to be in a tableview controller, but I need to display both the UItableView's at the same time which means it has to be within a UIView.

I can make the interface look the way I need it to through the IB however when trying to compile and build I receive the error that requires the UITableView's to be within a UITableViewController and not a UIView Controller. I have seen many examples using a master-detail layout, but the only stipulation is that this UITableview NEEDS to be displayed 100% of the time when in this view.

So basically, I am asking for direction... but a code example never hurt either! Thank you 100x's over!

-Jonathan

解决方案

UITableViewController is just a specialized UIViewController specially designed to display full screen UITableViews. It is (quite) equivalent to use an UITableViewController subclass or an UIViewController <UITableViewDataSource, UITableViewDelegate> subclass to manage a tableview.

So even if UITableViewController has some more spiecialized behaviors (automatically creates the UITableView if it does not exists, scrolls it automatically to display the keyboard, sets itself as the delegate and dataSource of the unique UITableView it manages, etc), you can use a standard UIViewController to manage a UITableView and be its dataSource to fill it.

That's even a way to manage a tableview that is not taking the full screen (as UITableViewController expects its view property to directly be the UITableView it manages, not a subview of its main view or whatever, and thus expects the UITableView to take the whole screen, contrary to using an UIViewController that has an UITableView as a custom-sized subclass of its view)


So in your case, you can have an UIViewController that has two IBOutlets, one for each tableView, and that unique UIViewController can be the dataSource (and delegate) of both the UITableViews. That's not a problem. Just be careful then in your datasource methods to distinguish if you are returning data for the first or the second UITableView to feed the correct tables each time.

@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;
}

这篇关于在UIView(UIView控制器)中加载多个tableviews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆