123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- //
- // SocialTableViewController.m
- // NIMDemo
- //
- // Created by Fenix Wang on 2017/6/20.
- // Copyright © 2017年 Netease. All rights reserved.
- //
- #import "SocialTableViewController.h"
- #import "SocialPublishViewController.h"
- #import "SocialTableViewCell.h"
- #import "HttpRequest.h"
- #import "UIView+NTES.h"
- @interface SocialTableViewController () <UIScrollViewDelegate>
- @property (nonatomic, strong) NSMutableArray *dataList;
- @property (nonatomic, assign) BOOL isFetching;
- @property (nonatomic, assign) BOOL flagRefresh;
- @end
- @implementation SocialTableViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- // Uncomment the following line to preserve selection between presentations.
- // self.clearsSelectionOnViewWillAppear = NO;
-
- // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
- // self.navigationItem.rightBarButtonItem = self.editButtonItem;
- _flagRefresh = NO;
- [self fetchDataList];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void)viewWillAppear:(BOOL)animated{
- [super viewWillAppear:animated];
-
-
- UINavigationItem *navItem = self.parentViewController.navigationItem;
-
- navItem.title = @"朋友圈";
-
- UIButton *emptyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [emptyBtn setTitle:@"" forState:UIControlStateNormal];
- [emptyBtn sizeToFit];
- UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:emptyBtn];
-
- [navItem setLeftBarButtonItems:@[leftItem]];
-
- UIButton *publishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [publishBtn setTitle:@"发布" forState:UIControlStateNormal];
- [publishBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
- [publishBtn addTarget:self action:@selector(onTouchPublish:) forControlEvents:UIControlEventTouchUpInside];
- [publishBtn sizeToFit];
-
- UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:publishBtn];
- [navItem setRightBarButtonItems:@[rightItem]];
- }
- - (void)fetchDataList{
-
- if(_isFetching)
- return;
-
- _isFetching = YES;
-
- NSString *lastId = nil;
- if(self.dataList && self.dataList.count){
- SocialItemData *lastData = [self.dataList lastObject];
- lastId = lastData.socialId;
- }
-
-
- [[HttpRequest shared] getSocialDataList:lastId success:^(NSMutableArray *dataList) {
- if(!self.dataList)
- self.dataList = dataList;
- else
- [self.dataList addObjectsFromArray:dataList];
-
- [self.tableView reloadData];
-
- _isFetching = NO;
- } failure:^{
- _isFetching = NO;
- }];
- }
- #pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- #warning Incomplete implementation, return the number of sections
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- #warning Incomplete implementation, return the number of rows
- if(_dataList)
- return _dataList.count;
- return 0;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-
- NSString *cellId = @"social";
- SocialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
- if(!cell)
- {
- cell = [[SocialTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
- [cell initWidth:self.view.frame.size.width];
- }
-
- SocialItemData *data = [_dataList objectAtIndex:indexPath.row];
- [cell setData:data];
-
- return cell;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
-
- SocialItemData *data = [self.dataList objectAtIndex:indexPath.row];
- return data.cellHeight;
- }
- /*
- // Override to support conditional editing of the table view.
- - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
- // Return NO if you do not want the specified item to be editable.
- return YES;
- }
- */
- /*
- // Override to support editing the table view.
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
- if (editingStyle == UITableViewCellEditingStyleDelete) {
- // Delete the row from the data source
- [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
- } else if (editingStyle == UITableViewCellEditingStyleInsert) {
- // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
- }
- }
- */
- /*
- // Override to support rearranging the table view.
- - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
- }
- */
- /*
- // Override to support conditional rearranging of the table view.
- - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
- // Return NO if you do not want the item to be re-orderable.
- return YES;
- }
- */
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- - (void)onTouchPublish:(int)sender
- {
- UIViewController *vc = [[SocialPublishViewController alloc] init];
- [self.navigationController pushViewController:vc animated:YES];
- }
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
- //康海涛测试Ok的
- CGPoint offset1 = scrollView.contentOffset;
- CGRect bounds1 = scrollView.bounds;
- CGSize size1 = scrollView.contentSize;
- UIEdgeInsets inset1 = scrollView.contentInset;
- float y1 = offset1.y + bounds1.size.height - inset1.bottom;
- float h1 = size1.height;
-
- //NSLog(@"%f %f %i %f", y1, h1, _flagRefresh, bounds1.size.height);
-
- if(h1 < bounds1.size.height)
- return;
-
- if (y1 > h1 + 50) {
- _flagRefresh = YES;
- }
- else if (y1 == h1) {
- if(_flagRefresh)
- {
- _flagRefresh = NO;
- NSLog(@"下拉刷新");
- [self fetchDataList];
- }
- }
-
- }
- @end
|