如图所示:说明tableView是分组显示的,每组的头部显示日期,row显示cell。
cell中可能存在按钮,则需要使用block将点击事件传到控制器。
程序代码如下:
1、初始化tableView
- (void)initWithTableView {
self.myTableView.dataSource = self;
self.myTableView.delegate = self;
[self.myTableView registerNib:[UINib nibWithNibName:[QHBuriedTreasureHistoryTableViewCell identifier] bundle:nil] forCellReuseIdentifier:[QHBuriedTreasureHistoryTableViewCell identifier]];
self.myTableView.tableFooterView = [UIView new];
_listDataArray = [NSMutableArray arrayWithCapacity:1];
[self prepareRefresh];
self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_myTableView.mj_footer.hidden =YES;
}
2、刷新动画(上拉加载、下拉刷新)
#pragma mark 刷新动画
- (void)prepareRefresh {
NSMutableArray *headerImages = [NSMutableArray array];
for (int i = 0; i <= 12; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading_%d",i]];
[headerImages addObject:image];
}
MJRefreshGifHeader *gifHeader = [MJRefreshGifHeader headerWithRefreshingBlock:^{
//下拉刷新要做的操作.
_page = 1;
[self loadRecordsRequest];
}];
[gifHeader setImages:@[headerImages[0]] forState:MJRefreshStateIdle];
[gifHeader setImages:headerImages forState:MJRefreshStateRefreshing];
_myTableView.mj_header = gifHeader;
NSMutableArray *footerImages = [NSMutableArray array];
for (int i = 0; i <= 12; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading_%d",i]];
[footerImages addObject:image];
}
MJRefreshAutoGifFooter *gifFooter = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
_page ++;
[self loadRecordsRequest];
}];
[gifFooter setImages:@[footerImages[0]] forState:MJRefreshStateIdle];
[gifFooter setImages:footerImages forState:MJRefreshStateRefreshing];
_myTableView.mj_footer = gifFooter;
}
3、viewDidLoad中
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"";
_page = 1;
[self initWithTableView];
[self loadRecordsRequest];
// Do any additional setup after loading the view from its nib.
}
4、调数据接口
#pragma mark 记录接口
- (void)loadRecordsRequest {
[MBProgressHUD showHUDAddedTo:self.view withTitle:@"" animated:YES];
QHTranferListRequset *listApi = [[QHTranferListRequset alloc]init];
if (listApi.cacheJson) {
[self objectWithJson:listApi.cacheJson];
}
listApi.page =[NSString stringWithFormat:@"%ld",(long)_page];
listApi.pageSize=pageSizeDefault;
listApi.orderType =@"5";//寻宝:5
listApi.paymentType = @"14";
listApi.ignoreCache = YES;
[listApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest *request) {
NSLog(@"%@", request.responseJSONObject);
[self objectWithJson:request.responseJSONObject];
} failure:^(__kindof YTKBaseRequest *request) {
// [self showNodateView:_myTableView AndQHOverlayPromptType:QHOverlayPromptConnectionError];
[MBProgressHUD hideHUDForView:self.view animated:YES];
[self.myTableView.mj_footer endRefreshing];
[self.myTableView.mj_header endRefreshing];
}];
}
5、解析数据
- (void)objectWithJson:(id)json{
[self removiewNoda];
[self.myTableView.mj_footer endRefreshing];
[self.myTableView.mj_header endRefreshing];
[MBProgressHUD hideHUDForView:self.view animated:YES];
if (QH_VALIDATE_REQUEST(json)) {
_model =[QHTranferListModel mj_objectWithKeyValues:json];
if (_page==1) {
[_listDataArray removeAllObjects];
}
[_listDataArray addObjectsFromArray:_model.data];
[self groupWithData];
if (_listDataArray.count == 0) {
[self showNodateView:_myTableView AndQHOverlayPromptType:QHOverlayPromptNoData];
}else{
_myTableView.mj_header.hidden =NO;
[self removiewNoda];
}
[_myTableView reloadData];
if (_listDataArray.count < [pageSizeDefault intValue]) {
self.myTableView.mj_footer.hidden = YES;
}else {
self.myTableView.mj_footer.hidden = NO;
}
}else{
NSString * msg =[json objectForKey:@"msg"];
[MBProgressHUD showMessag:msg toView:self.view afterDelay:1.0];
}
}
6、实现tableView的代理
#pragma mark UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [_resultArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *arr = _resultArray[section];
return arr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 25;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MainScreenScale, 25)];
view.backgroundColor = BgColor;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 200, 25)];
label.font = [UIFont systemFontOfSize:10];
label.textColor = UIColorFromRGB(0x969699);
QHTranferDetailInfoModel *model = _resultArray[section][0];
label.text = [NSObject timechange:model.createAt withFormat:@"yyyy年MM月"];
[view addSubview:label];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 61;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
QHBuriedTreasureHistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[QHBuriedTreasureHistoryTableViewCell identifier]];
QHTranferDetailInfoModel *infoModel =_resultArray[indexPath.section][indexPath.row];
cell.infoModel = infoModel;
__weak typeof(self) weakSelf = self;
[cell payClick:^(QHTranferDetailInfoModel *infoModel) {
__strong typeof(weakSelf) strongSelf = weakSelf;
[strongSelf paymentClicked:infoModel];
}];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
7、定义的属性
#define pageSizeDefault @"10"
@interface QHTreasureFindIncomeListViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *listDataArray;
@property (nonatomic, strong) NSMutableArray *resultArray;
@property (nonatomic, assign)NSInteger page;
@property (strong, nonatomic) QHTranferListModel * model;
@end
8、请求的数据
{
data = (
{
amount = 1;
amountStr = 1;
callbackState = 6;
callbackTime = 5;
createAt = 1473149701000;
currency = PAI;
id = 5284;
orderId = 50582FCB16664ECA9FF547B4C144FB23;
orderType = 5;
paymentType = 13;
receiveAddress = pt;
remark = "\U6362\U56fe\U8ba2\U5355:----1482";
sendAddress = pAqt;
state = 2;
updateAt = 1473149818000;
userId = 1;
}
);
status = success;
success = 1;
}
9、关于cell的按钮的操作:我们只需将model传出来即可
.h
- (void)payClick:(void(^)(QHTranferDetailInfoModel *infoModel))payBlock;
.m
@property (nonatomic, copy) void(^payBlock)(QHTranferDetailInfoModel *infoModel);
- (void)payClick:(void (^)(QHTranferDetailInfoModel *))payBlock {
_payBlock = payBlock;
}
- (IBAction)didPayClicked:(id)sender {
if (_payBlock) {
_payBlock(_infoModel);
}
}
10、最后在控制器处理
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
QHBuriedTreasureHistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[QHBuriedTreasureHistoryTableViewCell identifier]];
QHTranferDetailInfoModel *infoModel =_resultArray[indexPath.section][indexPath.row];
cell.infoModel = infoModel;
__weak typeof(self) weakSelf = self;
[cell payClick:^(QHTranferDetailInfoModel *infoModel) {
__strong typeof(weakSelf) strongSelf = weakSelf;
[strongSelf paymentClicked:infoModel];
}];
return cell;
}
#pragma mark 支付
- (void)paymentClicked:(QHTranferDetailInfoModel *)infoModel {
}
10、滚动到顶部
[_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];