Pet*_*erd 3 uitableview uiview ios
我试图将UITableView放入UIView.UIView也将包含其他一些东西,所以我想要定义UITiew的一部分以供UITableView使用.在这个和其他网站上阅读,我使用"UIViewController"并委托"".
我的问题是:
为什么我看不到任何带有下面代码的UITable?
当我更改InformatieSectieViewController.h时,为什么会看到UITable
@interface InformatieSectieViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
Run Code Online (Sandbox Code Playgroud)
至
@interface InformatieSectieViewController : UIViewTableController <UITableViewDataSource, UITableViewDelegate>
Run Code Online (Sandbox Code Playgroud)
(UIViewController到UI Table ViewController)?(这个UITable然后占据了整个UIView).
如何将UITableView正确地放入UIView中.
什么也让我感到惊讶; 当我从@interface InformatieSectieViewController:UIViewController <UITableViewDataSource,UITableViewDelegate>中删除<UITableViewDataSource,UITableViewDelegate>部分时,我希望行上的警告(在InformatieSectieViewController.m中):
tabView.delegate = self;
tabView.dataSource = self;
Run Code Online (Sandbox Code Playgroud)
但我没有得到这个警告.
这是我正在使用的代码:
InformatieSectie.h
#import <UIKit/UIKit.h>
@interface InformatieSectie : NSObject
@property (strong, nonatomic) NSString *header;
-(UIView*)createInformatieSectie;
@end
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
这是我定义UITableView的第二个类:
InformatieSectieViewController.h
#import <Foundation/Foundation.h>
@interface InformatieSectieViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tabView;
@end
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
几点意见:
您询问:
当我
<UITableViewDataSource, UITableViewDelegate>从中删除部件时Run Code Online (Sandbox Code Playgroud)@interface InformatieSectieViewController : UIViewController < UITableViewDataSource, UITableViewDelegate>我希望在线上发出警告(在InformatieSectieViewController.m中):
Run Code Online (Sandbox Code Playgroud)tabView.delegate = self; tabView.dataSource = self;但我没有得到这个警告.
是的,这很奇怪.如果你使用UIViewController,你真的应该看到这些警告.如果您有其他警告,有时您将看不到一些后续警告.或者有时如果您编辑标题,请不要保存和/或构建,否则您可能看不到警告.或者尝试从"Build"菜单中选择"Clean",然后重新构建,看看是否收到警告.
无论如何,即使您没有看到这些警告,您仍应将其定义为符合这两个协议,因为它将使您在Xcode中的代码更容易完成.
但是,如果使用a UITableViewController作为基类,则不需要将其定义为符合这两个协议,因为它已经符合UITableViewDataSource和UITableViewDelegate.
如何将tableview添加为子视图
并且回答更广泛的问题,即如何将表视图添加到包含其他内容的视图,而不是在以下方面执行以下操作loadView:
self.view = tabview;
Run Code Online (Sandbox Code Playgroud)
相反,您应该在以下方面执行以下操作loadView:
self.view = [[UIView alloc] init];
[self.view addSubview:tabview];
Run Code Online (Sandbox Code Playgroud)
或者,如果您不是以编程方式构建视图loadView,而是使用NIB或故事板,则可以在以下位置执行以下操作viewDidLoad:
[self.view addSubview:tabview];
Run Code Online (Sandbox Code Playgroud)
无论如何,通过确保您拥有另一个主视图,UIView并且您UITableView是子视图,您可以因此向该主视图添加其他控件.
查看和查看控制器层次结构
进一步查看代码示例,我看到您正在创建控制器,抓取其视图,将该视图添加为子视图,然后让控制器超出范围.这是一个非常糟糕的做法.如果是ARC,您将获得例外.如果非ARC,如果你忽略了这个视图,你就会泄漏(不是问题,如果这是你的顶级视图).
更糟糕的是,如果你这样做,你的视图控制器将不会收到某些事件(特别是旋转事件,但可能是其他事件).请参阅WWDC 2011 - 实现UIViewController Containment,以讨论保持视图和查看控制器层次结构同步的重要性.
如果这是您的顶级控制器,您应该将此控制器设置为rootViewController,例如在您的应用程序委托中:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
如果此视图控制器不是顶级控制器,您可以在呈现控制器中创建它,并从呈现视图控制器执行模态或推送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];
}
Run Code Online (Sandbox Code Playgroud)
或者,万一您使用自定义容器视图控制器,您可以执行以下操作:
- (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];
}
Run Code Online (Sandbox Code Playgroud)
这三种技术中的任何一种都将确保您的视图控制器层次结构与视图层次结构同步.我知道这似乎有点矫枉过正,但是如果你不用这三种方式之一来处理它,你会遇到问题.
我知道这里有很多,但我希望它有所帮助.如果它令人困惑,我道歉.
| 归档时间: |
|
| 查看次数: |
10097 次 |
| 最近记录: |