OK,小姐姐又来搞事情了,最近正在重构项目,今天就来分享一下项目中baseViewController的写法吧。当然可以视具体业务逻辑进行相应的增删改好吧。
直接上代码吧,直观点咯。
#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;
// 加载数据
-(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
首先主要是一个全局的导航栏咯---NavView (自己封装的,基本适应90%项目需求) 如果导航栏过于奇葩请老老实实再写一个吧,如果很多地方也是一样的奇葩样式建议封装成组件。
接下来就是一个tableview,就小姐姐的项目,90%的页面带tableview,所以把他写在基类里也挺方便的,当然如果没有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];//清内存
[[SDImageCache sharedImageCache] clearDisk];//清内存
[[SDImageCache sharedImageCache] cleanDisk];//清内存
// 1.取消正在下载的操作
[[SDWebImageManager sharedManager] cancelAll];
// 2.清除内存缓存
[[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 - 导航栏处理
- (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];
}
/**
* 导航栏左边按钮点击事件
*/
- (void)navViewClickLeftBtn:(NavView *)navView
{
[self.navigationController popViewControllerAnimated:YES];
}
/**
* 导航栏右边按钮点击事件
*/
- (void)navViewClickRightBtn:(NavView *)navView
{
[self.navigationController popViewControllerAnimated:YES];
}
@end
内容应该都能看懂哦,我挺相信你们的。那看看如何在页面中使用吧。
首先,创建的时候当然是要继承基类啦,要不你写基类干啥对吧😒
#import <UIKit/UIKit.h>
@interface HQMyOrderMainVC : YVBaseViewController
@end
添加导航条只需要
// 添加导航条
[self initNavViewWithRightStr:nil andNavTitle:@"我的订单" andType: NavViewTypeLeftIcon andNavBackgroundColor:nil andTitleColor:nil];
视业务需求决定是否需要上拉下拉功能
// 添加下拉刷新和上拉加载
[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:@"请求数据失败!" For:self.view yOffset:0];
}];
}
对了如果一进页面没有数据记得调用一下
[self.tableView.mj_header beginRefreshing];
其它就不说了,有什么想法再来和小姐姐交流。吃饭去了,饿死。