Ios 如何创建具有TableView、Search和;一些编辑函数,以便其他类可以从中继承

Ios 如何创建具有TableView、Search和;一些编辑函数,以便其他类可以从中继承,ios,iphone,uitableview,Ios,Iphone,Uitableview,我的项目中有许多UITableViewController类。他们有, 大部分类似的功能包括搜索、编辑、拉刷新 桌面视图 搜索栏 UISearchDisplayController 由于我不想在每个类中重新编写所有这些函数,因此如何创建具有所有这些函数的自定义基类 我不知道怎么做 任何教程链接或一些指导都会有很大帮助。更好的方法是创建一个超级类,比如说BaseTableViewController在此类中实现基本功能,并从此类继承其他UITableViewController类。像

我的项目中有许多UITableViewController类。他们有,

  • 大部分类似的功能包括搜索、编辑、拉刷新
  • 桌面视图
  • 搜索栏
  • UISearchDisplayController
由于我不想在每个类中重新编写所有这些函数,因此如何创建具有所有这些函数的自定义基类

我不知道怎么做


任何教程链接或一些指导都会有很大帮助。

更好的方法是创建一个超级类,比如说
BaseTableViewController
在此类中实现基本功能,并从此类继承其他
UITableViewController
类。像

    @interface BaseTableViewController : UITableViewController
     - (void)doSomething
    @end
另外,您的其他UITableViewController类应该如下

    @interface FirstTableViewController : BaseTableViewController

并调用
[super doSomething]

您需要遵循OOPS的简单继承概念

下面是一个简单的想法

  • 创建一个新文件,将其命名为
    myCustomTableBase
    &在该对话框的
    子类中键入
    UITableViewController
  • 现在在这个基类中提供所有与表相关的东西&公共方法&对象
  • 现在再次创建一个新文件,如
    step1
    &将其命名为
    myNewHomeController
    &在该对话框的
    部分的
    子类中键入
    myCustomTableBase
  • 处理完此文件后,可以检查
    myNewHomeController.h
    文件中的继承路径,如下所示:

    #import "myCustomTableBase.h"
    @interface myNewHomeController : myCustomTableBase
    @end
    
  • 现在取决于您如何设计您的基础&然后从中继承(子类)您的子类

    希望有帮助

    更新代码

    请注意:这里的基本调用来自
    UIviewController
    ,而不是
    UITableViewController

    BaseViewController.h

    @interface btBaseViewController : UIViewController {
     UITableViewStyle _tableViewStyle;
     UISearchDisplayController *_searchController;
    }
    @property (nonatomic, strong) UITableView *tableView;
    @property (nonatomic, strong) UISearchBar *searchBar;
    
    -(void)renderPadUI;   // should be implemented by derived class
    -(void)renderPhoneUI; // should be implemented by derived class
    
    #import "btBaseViewController.h"
    
    @interface btHomeViewController : btBaseViewController
    
    @end
    
    BaseViewController.m

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self renderUI];
    }
    
    
    -(void)renderUI{
         // Do any additional setup after loading the view.
        if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
        self.edgesForExtendedLayout = UIRectEdgeNone;
    
        if(IS_IPAD) {
            [self renderPadUI];
        }else {
            [self renderPhoneUI];
        }
    }
    
    -(void)enableSearchSetUp:(BOOL)show {
    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.bounds.size.width, 0)];
        _searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        _searchBar.delegate = self;
        _searchBar.placeholder = @"Search any keyword";
        [_searchBar sizeToFit];
    
        // Create search controller
        _searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
        _searchController.searchResultsDataSource = self;
        _searchController.searchResultsDelegate   = self;
        _searchController.delegate                = self;
    
        // add tableView
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:_tableViewStyle];
        _tableView.delegate         = self;
        _tableView.dataSource       = self;
        _tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
        [self.view addSubview:_tableView];
    
        // This is important line
        _tableView.tableHeaderView = _searchBar;
    
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }
    
    // you need not to call this method from this class. The base/parent class will invoke this method, as soon as you create the instane of this class.
    
    -(void)renderPhoneUI {
       [self enableSearchSetup:YES]; // this will base version
    }
    
    现在,在这个类的任何派生类中说
    btHomeViewController

    btHomeViewController.h

    @interface btBaseViewController : UIViewController {
     UITableViewStyle _tableViewStyle;
     UISearchDisplayController *_searchController;
    }
    @property (nonatomic, strong) UITableView *tableView;
    @property (nonatomic, strong) UISearchBar *searchBar;
    
    -(void)renderPadUI;   // should be implemented by derived class
    -(void)renderPhoneUI; // should be implemented by derived class
    
    #import "btBaseViewController.h"
    
    @interface btHomeViewController : btBaseViewController
    
    @end
    
    btHomeViewController.m

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self renderUI];
    }
    
    
    -(void)renderUI{
         // Do any additional setup after loading the view.
        if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
        self.edgesForExtendedLayout = UIRectEdgeNone;
    
        if(IS_IPAD) {
            [self renderPadUI];
        }else {
            [self renderPhoneUI];
        }
    }
    
    -(void)enableSearchSetUp:(BOOL)show {
    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.bounds.size.width, 0)];
        _searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        _searchBar.delegate = self;
        _searchBar.placeholder = @"Search any keyword";
        [_searchBar sizeToFit];
    
        // Create search controller
        _searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
        _searchController.searchResultsDataSource = self;
        _searchController.searchResultsDelegate   = self;
        _searchController.delegate                = self;
    
        // add tableView
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:_tableViewStyle];
        _tableView.delegate         = self;
        _tableView.dataSource       = self;
        _tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
        [self.view addSubview:_tableView];
    
        // This is important line
        _tableView.tableHeaderView = _searchBar;
    
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }
    
    // you need not to call this method from this class. The base/parent class will invoke this method, as soon as you create the instane of this class.
    
    -(void)renderPhoneUI {
       [self enableSearchSetup:YES]; // this will base version
    }
    

    • 请注意,您必须实现
      -(void)renderPhoneUI
      方法,因为基类希望子类实现此方法。或者把它从底座上拆下来

    • 您必须为
      UITableView
      &
      UISearchBar
      实现
      委托
      &
      数据源
      ,因为我没有在base中提供它的任何实现。如果在基类中给出委托的实现,那么派生类可以引用同一委托(如果不是在派生类中具体实现的话)

    • IS\u IPAD
      是一个自定义宏,用于使用
      UIDevice
      类检测设备版本。搜索它,你会得到它的定义

    • 这种方法提供了一个代码来处理带有基类中的搜索的TableView实现。如果要在派生类中对tableView和搜索栏进行任何自定义,则可以在特定类的
      renderPhoneUI
      方法中进行自定义

    • 我建议在基类本身中实现所有的
      委托。然后,您在整个应用程序中查看和搜索的所有内容都将保持一致。只需继续播放/更新
      数据源

    • 如果每个类的表视图在
      cellforrowatinexpath
      实现方面不同,则在所需类中提供
      cellforrowatinexpath
      方法的实现

    我希望我已经把更多的事情讲清楚了


    希望有帮助

    只需创建从UITableViewController继承的基本控制器MyTableViewController,并使其他控制器从中继承,而不是从中继承UITableViewController@JeromeDiaz但是UISearchBar、edit函数和pull to refresh控件怎么样?如何用这些内置的东西来创建这个基础控制器?@yongho:你应该把你的问题分成逻辑部分。1) 继承2)UIsearch栏3)带有刷新控件的UITableView。然后再把这些碎片拼在一起。您所问的主要是关于继承的问题,所以我们将向您提供有关继承设计的信息。@BalramTiwari所以,我想问一下如何将这些片段组合在一起。@yongho:好的。我给你拿1号和2号。刷新您从终端尝试的控件。我会更新我的答案,借几分钟时间。我知道这一点。但我不知道如何添加搜索栏,拉刷新控件,在基本控制器中编辑函数,这样子类就不必再次重写这些。基本上,我只是在基本控制器中编写这些函数,但只在子类中调用它?很好的方法。@yongho:最通用的特性应该朝上,即父级&更具体的特性应该在层次结构中向下移动&在子级中实现。子类与父类不同,因为它有自己的东西,其余的都是从父类继承的。如果示例项目对您有所帮助,请不要忘记投票。