简体   繁体   English

如何为所有视图控制器创建通用的UITableView?

[英]How to create common UITableView for all view controllers?

Does anyone have an idea about how to use a single UITableView in multiple ViewControllers ? 有谁知道如何在多个ViewControllers使用单个UITableView吗?

I need to create a common UITableView for all my view controllers and use it through out the application. 我需要为所有视图控制器创建一个通用的UITableView ,并在整个应用程序中使用它。

Any help is appreciated. 任何帮助表示赞赏。

Use ContainerView , you can drag ContainerView into your ViewControllers in a storyboard, adjust the size according what you need. 使用ContainerView ,您可以将ContainerView拖到情节提要中的ViewController中,并根据需要调整大小。 You use the ContainerView in all that ViewController that need the TableView . 您可以在所有需要TableView的 ViewController中使用ContainerView

Attached is a screen shot of one of my app that is using ContainerView that contains a TableView inside the ViewController. 附件是我的一个正在使用ContainerView的应用程序的屏幕快照,该应用程序在ViewController中包含TableView I am using the same thing in 2 different ViewControllers . 我在2个不同的ViewControllers中使用相同的东西。

在此处输入图片说明

You can use singleton. 您可以使用单例。 Here is an example: 这是一个例子:

CommonTableView.h file: CommonTableView.h文件:

#import <Foundation/Foundation.h>

@interface CommonTableView : UITableView <UITableViewDelegate>

+ (CommonTableView *)sharedInstance;

@end

CommonTableView.m file: CommonTableView.m文件:

#import “CommonTableView.h”

CommonTableView *sharedInstance = nil;

@implementation CommonTableView

+ (CommonTableView *)sharedInstance
{    
    static dispatch_once_t pred;

    dispatch_once(&pred, ^{
        sharedInstance = [[super allocWithZone:nil] init];
    });

    return sharedInstance;
}

- (id)init
{
    self = [super init];
    if (self) {
        self.delegate = self;
        //customize your table view
    }

    return self;
}

#pargma mark - UITableView delegate
//…

@end

Then you can access your tableView with [CommonTableView sharedInstance] in the UIViewControllers that you want. 然后,您可以使用所需的UIViewControllers中的[CommonTableView sharedInstance]访问tableView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM