ios - How to create a custom base class which has a TableView, a Search & some edit functions, so as other class can inherit from it -
i have many uitableviewcontroller classes in project. have,
- mostly similar functions including search, edit, pull refresh.
- a tableview
- a uisearchbar
- a uisearchdisplaycontroller.
as don't want re-write these functions each class, how create custom base class has these function.
i don't know how this?
any tutorial link or guidance of help.
you need follow simple inheritance concept of oops.
so here simple idea proceed.
- create new file, name
mycustomtablebase
& typeuitableviewcontroller
insubclass of
section in dialog. - now provide table related stuff & public method & objects in base class.
- now create new file again
step1
& namemynewhomecontroller
& typemycustomtablebase
insubclass of
section in dialog. once done file, can check
mynewhomecontroller.h
file inheritance path this:#import "mycustomtablebase.h" @interface mynewhomecontroller : mycustomtablebase @end
now how design base & subsequently inherit (subclass) child classes out of it.
hope helps.
updated code
please note: here base call derrived uiviewcontroller
, not uitableviewcontroller
baseviewcontroller.h
@interface btbaseviewcontroller : uiviewcontroller { uitableviewstyle _tableviewstyle; uisearchdisplaycontroller *_searchcontroller; } @property (nonatomic, strong) uitableview *tableview; @property (nonatomic, strong) uisearchbar *searchbar; -(void)renderpadui; // should implemented derived class -(void)renderphoneui; // should implemented derived class
baseviewcontroller.m
- (void)viewdidload { [super viewdidload]; [self renderui]; } -(void)renderui{ // additional setup after loading 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 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]; // important line _tableview.tableheaderview = _searchbar; }
now in of derived class class bthomeviewcontroller
bthomeviewcontroller.h
#import "btbaseviewcontroller.h" @interface bthomeviewcontroller : btbaseviewcontroller @end
bthomeviewcontroller.m
- (void)viewdidload { [super viewdidload]; // additional setup after loading view. } // need not call method class. base/parent class invoke method, create instane of class. -(void)renderphoneui { [self enablesearchsetup:yes]; // base version }
also
note have implement
-(void)renderphoneui
method base class expecting child implement this. or else remove base then.you have implement
delegates
&datasource
uitableview
&uisearchbar
, because haven't provided implementation of in base. if make give implementation of delegates in base class, derived class can refer same delegate's if not implemented in derived class.is_ipad
custom macro detect device version usinguidevice
class. search it, definition.this approach gives single code handles tableview implementation search in base class. if want customization tableview & search bar in derived class, can customization in specific class's
renderphoneui
method.i suggest implement
delegates
in base class itself. tableview & search, across app consistent. keep playing/updatingdatasource
.if tableview different each class, in terms of
cellforrowatindexpath
implementation, provide implementation ofcellforrowatindexpath
method in intended class.
i hope have made more of things clean & clear.
hope helps.
Comments
Post a Comment