238 lines
6.2 KiB
Objective-C
Executable File
238 lines
6.2 KiB
Objective-C
Executable File
//
|
||
// PYTableViewController.m
|
||
// Food
|
||
//
|
||
// Created by ko1o on 2019/7/25.
|
||
// Copyright © 2019年 ko1o. All rights reserved.
|
||
//
|
||
|
||
#import "PYTableViewController.h"
|
||
#import "MJRefresh.h"
|
||
#import "MTPageView.h"
|
||
|
||
@implementation PYTableCell
|
||
|
||
- (void)setupUI {
|
||
self.selectionStyle = UITableViewCellSelectionStyleNone;
|
||
}
|
||
|
||
- (instancetype)initWithFrame:(CGRect)frame {
|
||
if (self = [super initWithFrame:frame]) {
|
||
[self setupUI];
|
||
}
|
||
return self;
|
||
}
|
||
|
||
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
|
||
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
|
||
[self setupUI];
|
||
}
|
||
return self;
|
||
}
|
||
|
||
@end
|
||
|
||
@interface PYTableViewController ()
|
||
|
||
// 是否刷新过
|
||
@property (nonatomic, assign) BOOL didViewDidAppear;
|
||
|
||
@end
|
||
|
||
@implementation PYTableViewController
|
||
|
||
- (instancetype)init
|
||
{
|
||
if (self = [super init]) {
|
||
self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
|
||
self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(startRefresh)];
|
||
self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(startLoadMore)];
|
||
self.tableView.mj_footer.hidden = YES;
|
||
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
|
||
}
|
||
return self;
|
||
}
|
||
|
||
- (BOOL)zeroContentInset
|
||
{
|
||
return NO;
|
||
}
|
||
|
||
- (void)viewDidLayoutSubviews
|
||
{
|
||
[super viewDidLayoutSubviews];
|
||
|
||
self.tableView.frame = self.view.bounds;
|
||
|
||
[self adjustContetnInset];
|
||
}
|
||
|
||
- (BOOL)mt_shouldSetupStateful
|
||
{
|
||
return NO;
|
||
}
|
||
|
||
- (void)adjustContetnInset
|
||
{
|
||
if ([self zeroContentInset]) {
|
||
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, self.tableView.mj_footer.height, 0);
|
||
} else {
|
||
CGFloat bottomInset = self.tabBarController.tabBar && self.tabBarController.tabBar.hidden == NO ? self.tabBarController.tabBar.height : homeIndicatorHeight();
|
||
self.tableView.contentInset = UIEdgeInsetsMake(self.navigationController.navigationBar.bottom, 0, bottomInset + self.tableView.mj_footer.height, 0);
|
||
}
|
||
}
|
||
|
||
- (CGFloat)rowHegight {
|
||
return FIX_SIZE(68);
|
||
}
|
||
|
||
- (void)startLoading {
|
||
[super startLoading];
|
||
|
||
self.itemsM = [NSMutableArray array];
|
||
}
|
||
|
||
- (void)startRefresh
|
||
{
|
||
self.itemsM = [NSMutableArray array];
|
||
[self endLoading:nil];
|
||
[self.tableView.mj_footer endRefreshing];
|
||
[self.tableView.mj_footer resetNoMoreData];
|
||
}
|
||
|
||
- (void)startLoadMore
|
||
{
|
||
[self.tableView.mj_header endRefreshing];
|
||
}
|
||
|
||
- (BOOL)refreshEnable
|
||
{
|
||
return YES;
|
||
}
|
||
|
||
- (BOOL)autoRefreshWhenViewDidLoad
|
||
{
|
||
return YES;
|
||
}
|
||
|
||
- (BOOL)mt_nagationBarTransparent {
|
||
return NO;
|
||
}
|
||
|
||
- (void)viewDidLoad {
|
||
[super viewDidLoad];
|
||
|
||
self.tableView.frame = self.view.bounds;
|
||
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
|
||
self.tableView.delegate = self;
|
||
self.tableView.dataSource = self;
|
||
self.currentPage = 1;
|
||
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
|
||
self.tableView.estimatedRowHeight = 0;
|
||
self.tableView.estimatedSectionHeaderHeight = 0;
|
||
self.tableView.estimatedSectionFooterHeight = 0;
|
||
if (@available(iOS 15.0, *)) {
|
||
self.tableView.sectionHeaderTopPadding = 0;
|
||
}
|
||
[self.view addSubview:self.tableView];
|
||
self.tableView.backgroundColor = BG_COLOR;
|
||
|
||
if (self.autoBeginRefreshWhenViewDidLoad) {
|
||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||
[self.tableView.mj_header beginRefreshing];
|
||
});
|
||
}
|
||
}
|
||
|
||
- (void)viewWillAppear:(BOOL)animated
|
||
{
|
||
[super viewWillAppear:animated];
|
||
|
||
if (![self refreshEnable]) {
|
||
self.tableView.mj_header = nil;
|
||
}
|
||
|
||
if (![self loadMoreEnable]) {
|
||
self.tableView.mj_footer = nil;
|
||
}
|
||
}
|
||
|
||
- (BOOL)autoBeginRefreshWhenViewDidLoad {
|
||
return YES;
|
||
}
|
||
|
||
- (BOOL)loadMoreEnable
|
||
{
|
||
return YES;
|
||
}
|
||
|
||
//overrid 表示内容是否为空,返回若为NO,会显示空页面提醒
|
||
- (BOOL)mt_hasContentForStateful
|
||
{
|
||
NSInteger rowsCount = [self.tableView.dataSource tableView:self.tableView numberOfRowsInSection:0];
|
||
// self.tableView.hidden = rowsCount == 0;
|
||
|
||
return rowsCount;
|
||
}
|
||
|
||
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
|
||
{
|
||
return CGFLOAT_MIN;
|
||
}
|
||
|
||
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
|
||
return 1;
|
||
}
|
||
|
||
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
||
return self.itemsM.count;
|
||
}
|
||
|
||
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
|
||
{
|
||
return [UIView new];
|
||
}
|
||
|
||
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
|
||
{
|
||
[self endLoading:nil];
|
||
|
||
self.tableView.hidden = NO;
|
||
NSInteger rowsCount = [self.tableView.dataSource tableView:self.tableView numberOfRowsInSection:0];
|
||
self.tableView.mj_footer.hidden = rowsCount == 0;
|
||
// if (rowsCount >= self.totalCount && self.tableView.scrollEnabled) {
|
||
// [self.tableView.mj_footer endRefreshingWithNoMoreData];
|
||
// } else {
|
||
// if ([self loadMoreEnable]) {
|
||
// [self.tableView.mj_footer resetNoMoreData];
|
||
// }
|
||
// }
|
||
|
||
[self.tableView.mj_header endRefreshing];
|
||
[self adjustContetnInset];
|
||
return CGFLOAT_MIN;
|
||
}
|
||
|
||
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
|
||
{
|
||
return [UIView new];
|
||
}
|
||
|
||
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
|
||
return [self rowHegight];
|
||
}
|
||
|
||
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
||
NSString *cellID = NSStringFromClass([self cellClass]);
|
||
PYTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
|
||
if (!cell) {
|
||
cell = [[self cellClass] alloc];
|
||
cell = [cell initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
|
||
}
|
||
cell.selectionStyle = UITableViewCellSelectionStyleNone;
|
||
cell.model = self.itemsM[indexPath.row];
|
||
return cell;
|
||
}
|
||
|
||
@end
|