簡體   English   中英

如何將UITableView放入UIView中

[英]How do I put a UITableView into a UIView

我試圖將UITableView放入UIView。 UIView也將包含其他一些東西,所以我想要定義UITiew的一部分以供UITableView使用。 在這個和其他網站上閱讀,我使用“UIViewController”並委托“”。

我的問題是:

  • 為什么我看不到任何帶有下面代碼的UITable?

  • 當我更改InformatieSectieViewController.h時,為什么會看到UITable

     @interface InformatieSectieViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> 

     @interface InformatieSectieViewController : UIViewTableController <UITableViewDataSource, UITableViewDelegate> 

    (UIViewController到UI Table ViewController)? (這個UITable然后占據了整個UIView)。

  • 如何將UITableView正確地放入UIView中。

  • 什么也讓我感到驚訝; 當我從@interface InformatieSectieViewController:UIViewController <UITableViewDataSource,UITableViewDelegate>中刪除<UITableViewDataSource,UITableViewDelegate>部分時,我希望行上的警告(在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

幾點意見:

  1. 您詢問:

    當我從中刪除<UITableViewDataSource, UITableViewDelegate>部分時

      @interface InformatieSectieViewController : UIViewController < UITableViewDataSource, UITableViewDelegate> 

    我希望在線上發出警告(在InformatieSectieViewController.m中):

      tabView.delegate = self; tabView.dataSource = self; 

    但我沒有得到這個警告。

    是的,這很奇怪。 如果你使用UIViewController ,你真的應該看到這些警告。 如果您有其他警告,有時您將看不到一些后續警告。 或者有時,如果您編輯標題,請不要保存和/或構建,否則您可能看不到警告。 或者嘗試從“Build”菜單中選擇“Clean”,然后重新構建,看看是否收到警告。

    無論如何,即使您沒有看到這些警告,您仍應將其定義為符合這兩個協議,因為它將使您在Xcode中的代碼更容易完成。

    但是,如果使用UITableViewController作為基類,則不需要將其定義為符合這兩個協議,因為它已經符合UITableViewDataSourceUITableViewDelegate

  2. 如何將tableview添加為子視圖

    並且回答更廣泛的問題,即如何將表視圖添加到包含其他內容的視圖,而不是在loadView執行以下操作:

     self.view = tabview; 

    相反,您應該在loadView執行以下操作:

     self.view = [[UIView alloc] init]; [self.view addSubview:tabview]; 

    或者,如果您沒有在loadView以編程方式構建視圖,而是使用NIB或故事板,則可以在viewDidLoad執行以下操作:

     [self.view addSubview:tabview]; 

    無論如何,通過確保您擁有另一個主UIView ,並且您的UITableView是子視圖,您可以因此向該主視圖添加其他控件。

  3. 查看和查看控制器層次結構

    進一步查看代碼示例,我看到您正在創建控制器,抓取其視圖,將該視圖添加為子視圖,然后讓控制器超出范圍。 這是一個非常糟糕的做法。 如果是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; } 

    如果此視圖控制器不是頂級控制器,您可以在呈現控制器中創建它,並從呈現視圖控制器執行模態或推送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]; } 

    這三種技術中的任何一種都將確保您的視圖控制器層次結構與視圖層次結構同步。 我知道這似乎有點矯枉過正,但是如果你不用這三種方式之一來處理它,你會遇到問題。

我知道這里有很多,但我希望它有所幫助。 如果它令人困惑,我道歉。

您應該[self.view addSubview:self.tabView]而不是self.view = self.tabView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM