如圖所示:說明tableView是分組顯示的,每組的頭部顯示日期,row顯示cell。json
cell中可能存在按鈕,則須要使用block將點擊事件傳到控制器。ide
程序代碼以下:動畫
一、初始化tableViewatom
- (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; }
二、刷新動畫(上拉加載、下拉刷新)代理
#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; }
三、viewDidLoad中code
- (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @""; _page = 1; [self initWithTableView]; [self loadRecordsRequest]; // Do any additional setup after loading the view from its nib. }
四、調數據接口orm
#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]; }]; }
五、解析數據接口
- (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]; } }
六、實現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]; }
七、定義的屬性ci
#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
八、請求的數據
{ 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; }
九、關於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); } }
十、最後在控制器處理
- (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 { }
十、滾動到頂部
[_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];