OK,小姐姐又來搞事情了饿敲,最近正在重構(gòu)項目妻导,今天就來分享一下項目中baseViewController的寫法吧。當(dāng)然可以視具體業(yè)務(wù)邏輯進(jìn)行相應(yīng)的增刪改好吧怀各。
直接上代碼吧倔韭,直觀點咯。
#import <UIKit/UIKit.h>
@interface HQBaseViewController : UIViewController<NavViewDelegate>
@property (nonatomic,strong) NavView * navView;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,assign) NSInteger currentPage;
@property (nonatomic,assign) NSInteger preQueryPage;
// 加載數(shù)據(jù)
-(void)loadData;
//上下拉刷新之后處理
-(void)handleRefreshWithCurrentPage:(NSInteger)currentPage totalPage:(NSInteger)totalPage ;
//添加上下拉刷新
-(void)addTableViewHeaderRefresh:(BOOL)headerRefresh footerRefresh:(BOOL)footerRefresh;
- (void)initNavViewWithRightStr:(NSString *) rightStr andNavTitle:(NSString *) navTitle andType:(NavViewType) type andNavBackgroundColor:(UIColor *)bgColor andTitleColor:(UIColor *)titleColor;
@end
首先主要是一個全局的導(dǎo)航欄咯---NavView (自己封裝的瓢对,基本適應(yīng)90%項目需求) 如果導(dǎo)航欄過于奇葩請老老實實再寫一個吧寿酌,如果很多地方也是一樣的奇葩樣式建議封裝成組件。
接下來就是一個tableview硕蛹,就小姐姐的項目醇疼,90%的頁面帶tableview,所以把他寫在基類里也挺方便的法焰,當(dāng)然如果沒有tableview也無所謂秧荆,老老實實控制器該咋寫咋寫,還有就是把下拉刷新和上拉加載寫在基類里埃仪,那么就不需要每個有類似功能的頁面都寫一遍代碼辰如,雖然是copy也是挺煩的不是么?有沒有和我一樣copy也不愿意的小哥哥贵试!??
接下來看看.m文件把
#import "HQBaseViewController.h"
@interface HQBaseViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>
@end
@implementation HQBaseViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.edgesForExtendedLayout = UIRectEdgeNone;
self.view.backgroundColor = [UIColor whiteColor];
self.currentPage = 1;
if (!_dataArray) {
_dataArray = [NSMutableArray array];
}
}
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
[[SDImageCache sharedImageCache] clearMemory];//清內(nèi)存
[[SDImageCache sharedImageCache] clearDisk];//清內(nèi)存
[[SDImageCache sharedImageCache] cleanDisk];//清內(nèi)存
// 1.取消正在下載的操作
[[SDWebImageManager sharedManager] cancelAll];
// 2.清除內(nèi)存緩存
[[SDWebImageManager sharedManager].imageCache clearMemory];
}
-(void)addTableViewHeaderRefresh:(BOOL)headerRefresh footerRefresh:(BOOL)footerRefresh{
if (headerRefresh) {
self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction: @selector(loadNewData)];
}
if (footerRefresh) {
self.tableView.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}
}
-(void)loadNewData{
self.currentPage=1;
self.preQueryPage = 0;
[self.tableView.mj_footer resetNoMoreData];
[self loadData];
}
-(void)loadMoreData{
self.currentPage++;
[self loadData];
}
-(void)loadData{
}
-(void)handleRefreshWithCurrentPage:(NSInteger)currentPage totalPage:(NSInteger)totalPage {
if (totalPage>0) {
[self.tableView reloadData];
}
if (currentPage>=totalPage) {
[self.tableView.mj_footer endRefreshingWithNoMoreData];
}else{
if (self.preQueryPage != currentPage && currentPage > 0 && currentPage < totalPage && self.tableView.contentSize.height < CGRectGetHeight(self.tableView.frame)) {
[self loadMoreData];
}else{
[self.tableView.mj_footer endRefreshing];
}
}
self.preQueryPage = currentPage;
if ([self.tableView.mj_header isRefreshing]) {
[self.tableView.mj_header endRefreshing];
}
}
#pragma mark - 導(dǎo)航欄處理
- (void)initNavViewWithRightStr:(NSString *) rightStr andNavTitle:(NSString *) navTitle andType:(NavViewType) type andNavBackgroundColor:(UIColor *)bgColor andTitleColor:(UIColor *)titleColor
{
self.navView = [NavView navViewWithBackIcon:nil andRightStr:rightStr andNavTitle:navTitle andType:type andNavBackgroundColor:bgColor andTitleColor:titleColor];
self.navView.delegate=self;
[self.view addSubview:self.navView];
}
/**
* 導(dǎo)航欄左邊按鈕點擊事件
*/
- (void)navViewClickLeftBtn:(NavView *)navView
{
[self.navigationController popViewControllerAnimated:YES];
}
/**
* 導(dǎo)航欄右邊按鈕點擊事件
*/
- (void)navViewClickRightBtn:(NavView *)navView
{
[self.navigationController popViewControllerAnimated:YES];
}
@end
內(nèi)容應(yīng)該都能看懂哦,我挺相信你們的凯正。那看看如何在頁面中使用吧毙玻。
首先,創(chuàng)建的時候當(dāng)然是要繼承基類啦廊散,要不你寫基類干啥對吧??
#import <UIKit/UIKit.h>
@interface HQMyOrderMainVC : YVBaseViewController
@end
添加導(dǎo)航條只需要
// 添加導(dǎo)航條
[self initNavViewWithRightStr:nil andNavTitle:@"我的訂單" andType: NavViewTypeLeftIcon andNavBackgroundColor:nil andTitleColor:nil];
視業(yè)務(wù)需求決定是否需要上拉下拉功能
// 添加下拉刷新和上拉加載
[self addTableViewHeaderRefresh:YES footerRefresh:YES];
重寫一下load()方法
// 下拉刷新
- (void)loadData {
HQOrderBaseService *orderBaseService = [HQOrderBaseService defaultOrderBaseService];
[orderBaseService getOrderList:[NSString stringWithFormat:@"%ld",(long)page] OrderType:status Success:^(NSDictionary *params) {
[self.contentTbv.mj_header endRefreshing];
[self.contentTbv.mj_footer endRefreshing];
if (page==1) {
if (self.orderCountArr.count!=0) {
[self.orderCountArr removeAllObjects];
}
}
if (self.orderCountArr.count!=0) {
totalPage=[params[@"data"][@"pagination"][@"totalPage"] integerValue];
[self handleRefreshWithCurrentPage:self.currentPage totalPage:totalPage];
}
} Error:^(NSString *errorMsg) {
[MBProgressHUD Message:errorMsg For:self.view yOffset:0];
} Fail:^(NSError *error) {
[MBProgressHUD Message:@"請求數(shù)據(jù)失斏L病!" For:self.view yOffset:0];
}];
}
對了如果一進(jìn)頁面沒有數(shù)據(jù)記得調(diào)用一下
[self.tableView.mj_header beginRefreshing];
其它就不說了允睹,有什么想法再來和小姐姐交流运准。吃飯去了幌氮,餓死。