我正在尝试将UITableView放入UIView中。 UIView也将包含其他一些东西,所以我想要定义UITiew的一部分以供UITableView使用。阅读本网站和其他网站,我正在使用“UIViewController”并委托“”。
我的问题是:
为什么我看不到任何带有以下代码的UITable?
为什么在更改InformatieSectieViewController.h时会看到UITable
@interface InformatieSectieViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
到
@interface InformatieSectieViewController : UIViewTableController <UITableViewDataSource, UITableViewDelegate>
(UIViewController到UI 表 ViewController)? (这个UITable然后占据整个UIView)。
如何将UITableView正确地放入UIView中。
让我感到惊讶的是什么;当我删除&lt; UITableViewDataSource,UITableViewDelegate&gt;部分来自@interface InformatieSectieViewController:UIViewController&lt; UITableViewDataSource,UITableViewDelegate&gt;,我希望行上有警告(在InformatieSectieViewController.m中):
tabView.delegate = self;
tabView.dataSource = self;
但我没有得到这个警告。
以下是我正在使用的代码:
InformatieSectie.h
#import <UIKit/UIKit.h>
@interface InformatieSectie : NSObject
@property (strong, nonatomic) NSString *header;
-(UIView*)createInformatieSectie;
@end
InformatieSectie.m
#import "InformatieSectie.h"
#import "InformatieSectieViewController.h"
#import <QuartzCore/QuartzCore.h>
@implementation InformatieSectie
@synthesize header;
@synthesize footer;
-(UIView*)createInformatieSectie{
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 200, breedte, hoogte)];
//(add headerlabel at pos)
[view addSubview:headerLabel];
InformatieSectieViewController *svc = [[InformatieSectieViewController alloc] init];
[view addSubview:svc.view];
return view;
}
@end
这是我定义UITableView的第二个类:
InformatieSectieViewController.h
#import <Foundation/Foundation.h>
@interface InformatieSectieViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tabView;
@end
InformatieSectieViewController.m
#import "InformatieSectieViewController.h"
#import <QuartzCore/QuartzCore.h>
@implementation InformatieSectieViewController
@synthesize tabView;
-(void)loadView {
tabView = [[UITableView alloc] initWithFrame:CGRectMake(100, 100, 20, 20)];
tabView.delegate = self;
tabView.dataSource = self;
tabView.layer.borderWidth = 10.0;
tabView.layer.borderColor = [UIColor yellowColor].CGColor;
self.view = tabView;
self.view.layer.backgroundColor = [UIColor magentaColor].CGColor;
[super loadView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// (return some cell)
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// (Nothing yet)
}
@end
答案 0 :(得分:6)
有几点意见:
你问:
从
中删除<UITableViewDataSource, UITableViewDelegate>
部分时@interface InformatieSectieViewController : UIViewController < UITableViewDataSource, UITableViewDelegate>
我希望行上有警告(在InformatieSectieViewController.m中):
tabView.delegate = self; tabView.dataSource = self;
但我没有得到这个警告。
是的,这很奇怪。如果您使用UIViewController
,您真的应该看到这些警告。如果您有其他警告,有时您将看不到一些后续警告。或者有时,如果您编辑标题,请不要保存和/或构建,否则您可能看不到警告。或者尝试从“Build”菜单中选择“Clean”,然后重新构建,看看是否收到警告。
无论如何,即使您没有看到这些警告,您仍应将其定义为符合这两个协议,因为它将使您在Xcode中的代码更容易完成。
但是,如果使用UITableViewController
作为基类,则无需将其定义为符合这两个协议,因为它已符合UITableViewDataSource
和UITableViewDelegate
。
如何将tableview添加为子视图
回答更广泛的问题,即如何将表格视图添加到包含其他内容的视图中,而不是在loadView
中执行以下操作:
self.view = tabview;
相反,您应该在loadView
中执行以下操作:
self.view = [[UIView alloc] init];
[self.view addSubview:tabview];
或者,如果您没有在loadView
中以编程方式构建视图,而是使用NIB或故事板,则可以在viewDidLoad
中执行以下操作:
[self.view addSubview:tabview];
无论如何,通过确保您拥有另一个主UIView
,并且您的UITableView
是子视图,您可以在该主视图中添加其他控件。
查看和查看控制器层次结构
进一步研究你的代码示例,我看到你正在创建控制器,抓取它的视图,将该视图添加为子视图,然后让控制器超出范围。这是一个非常糟糕的做法。如果是ARC,您将获得例外。如果非ARC,如果你忽略了这个视图就会泄漏(不是问题,如果这是你的顶级视图)。
更糟糕的是,如果你这样做,你的视图控制器将不会收到某些事件(特别是轮换事件,但可能是其他事件)。有关保持视图和查看控制器层次结构同步的重要性的讨论,请参阅WWDC 2011 - Implementing UIViewController Containment。
如果这是您的顶级控制器,则应将此控制器设置为rootViewController
,例如在你的app delegate中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
// create your controller any way you want
self.viewController = [[InformatieSectieViewController alloc] init];
// if not using ARC, it should be
//
// self.viewController = [[[InformatieSectieViewController alloc] init] autorelease];
// by defining this as your root view controller, you'll be assured that the controller
// is now part of the controller hierarchy
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
如果此视图控制器不是顶级控制器,您可以在呈现控制器中创建它,并从呈现视图控制器执行模态或推送segue,例如:
- (void)transitionToNextViewController
{
UIViewController *controller = [[InformatieSectieViewController alloc] init];
// again, if not using ARC, that line should be:
//
// UIViewController *controller = [[[InformatieSectieViewController alloc] init] autorelease];
[self presentViewController:controller animated:YES completion:nil];
// or
// [self.navigationController pushViewController:controller animated:YES];
}
或者,万一你使用自定义容器视图控制器,你会做类似的事情:
- (void)addChild
{
UIViewController *controller = [[InformatieSectieViewController alloc] init];
// again, if not using ARC, that line should be:
//
// UIViewController *controller = [[[InformatieSectieViewController alloc] init] autorelease];
[self addChildViewController:controller];
controller.view.frame = CGRectMake(x, y, width, height);
[self.view addSubview:controller.view];
[controller didMoveToParentViewController:self];
}
这三种技术中的任何一种都将确保您的视图控制器层次结构将与您的视图层次结构同步。我知道这似乎有点矫枉过正,但如果你不以这三种方式之一处理它,你会遇到问题。
我知道这里有很多,但我希望它有所帮助。如果它令人困惑,我道歉。
答案 1 :(得分:0)
您应该[self.view addSubview:self.tabView]
而不是self.view = self.tabView