KWMHomeVC.m 5.35 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//
//  KWMHomeVC.m
//  iCemarose
//
//  Created by HouWeiBin on 16/8/22.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMHomeVC.h"
#import "KWMShopCartVC.h"
#import "KWMGuideVC.h"
#import "KWMBlogCell.h"
#import "KWMSearchBar.h"
#import "KWMSearchFeedbackVC.h"
#import "KWMShopCartData.h"
#import "KWMBlogDetailVC.h"
#import "KWMUserModel.h"
#import "KWMLoginVC.h"
lee committed
19
#import "KWMShoppingCart.h"
houweibin committed
20 21 22 23 24 25 26 27 28

@interface KWMHomeVC ()
@property (nonatomic)KWMSearchBar *searchBar;
@property (nonatomic)NSArray *dataArr;

@end

@implementation KWMHomeVC

u  
lee committed
29
+ (NSString *)kwmTag{
houweibin committed
30 31 32 33
    return @"KWMHomeVC";
}

- (void)viewDidLoad {
lee committed
34 35 36 37 38 39 40
//    KWMUserModel *user = [KWMUserModel shareUser];
//    if (user.status != 1) {
//        //登录
//        KWMLoginVC *loginVc = (KWMLoginVC *)[KWMBaseVC findControllerBy:[KWMLoginVC kwmTag] fromStoryboard:@"Login"];
//        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVc];
//        [self presentViewController:nav animated:YES completion:nil];
//    }
houweibin committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    
    
    [super viewDidLoad];
    _dataArr = [NSArray array];
    [self requestBlogAPI];
}

- (UITableView *)targetTableView{
    return self.tbvBlog;
}
- (void)loadData{
    [self requestBlogAPI];
}

- (void)reLoadData{
    [self requestBlogAPI];
}

//刷新用户
//- (void)refreshCustomer:(BUYCustomer *)customer{
//    [self.client updateCustomer:customer callback:^(BUYCustomer * _Nullable customer, NSError * _Nullable error) {
//         if (customer != nil &&error == nil) {
//             [[NSUserDefaults standardUserDefaults] setObject:customer forKey:@"customer"];
//         }else if (error != nil) {
//             [self showError:error];
//         }
//    }];
//}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
lee committed
72 73 74 75 76 77 78
    [self.navigationController setNavigationBarHidden:NO];
//    [self initHeaderView];
//    if (_searchBar) {
//        NSInteger count = [[KWMShoppingCart sharedInstance] count].integerValue;
//        _searchBar.count = count;
//    }
//    [_searchBar resumeView];
houweibin committed
79 80 81 82
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
lee committed
83 84
//    [self.navigationController setNavigationBarHidden:YES];
//    [_searchBar removeFromSuperview];
houweibin committed
85 86 87 88 89 90 91 92
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)awakeFromNib{
    [super awakeFromNib];
lee committed
93
    self.title = @"发现新鲜";
houweibin committed
94 95 96 97 98
}

//请求api
- (void)requestBlogAPI{
    __weak KWMHomeVC *weakSelf = self;
99 100 101 102 103 104
    [self.api getAllBlog:nil cacheCallback:^(KWMBlogResult *result) {
        if (result != nil) {
            _dataArr =  [[result.blogs reverseObjectEnumerator] allObjects];
            [self.tbvBlog reloadData];
        }
    }  success:^(NSURLSessionDataTask *task, KWMBlogResult *result) {
houweibin committed
105 106 107 108 109 110
        [weakSelf hideLoading];
        if (result != nil) {
            _dataArr =  [[result.blogs reverseObjectEnumerator] allObjects];
            [self.tbvBlog reloadData];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
111 112 113
        if(self.dataArr==nil || self.dataArr.count == 0){
            [self showError:error];
        }
houweibin committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
        NSLog(@"error:%@",error);
    }];
}

#pragma  mark UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat imageHeight = 211.0/375.0 * UI_SCREEN_WIDTH;
    CGFloat defaultHeight = 339;
    
    KWMArticlesResult *article = [_dataArr objectAtIndex:indexPath.row];
    if (article.imageScr == nil) {
        imageHeight = 0;
    }
    
    if(indexPath.row == 0) {
        defaultHeight = 354;
    }
    return ( defaultHeight - 221 + imageHeight);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *indentifier = @"KWMBlogCell";
    
    KWMBlogCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    
    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:indentifier bundle:nil] forCellReuseIdentifier:indentifier];
        cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    }
    
    KWMArticlesResult *article = [_dataArr objectAtIndex:indexPath.row];
    
    cell.article = article;
    [cell setData:indexPath.row];
    return cell;
}

//点击了某一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    KWMBlogDetailVC *blogDetailVC = (KWMBlogDetailVC *)[KWMBlogDetailVC findControllerBy:@"KWMBlogDetailVC" fromStoryboard:@"Home"];
    KWMArticlesResult *article = [_dataArr objectAtIndex:indexPath.row];
    blogDetailVC.article = article;
    [self.navigationController pushViewController:blogDetailVC animated:YES];
}

- (void)initHeaderView{
    //添加頭部header
    _searchBar = [[KWMSearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    _searchBar.delegate = self;
    _searchBar.btnSearch.hidden = YES;
    [self.view addSubview:_searchBar];
}


#pragma mark -- KWMSearchBarDelegate

- (void)kwm_onClickShopCar{
    //购物车
    KWMShopCartVC *contactVC = (KWMShopCartVC *)[KWMBaseVC findControllerBy:[KWMShopCartVC kwmTag] fromStoryboard:@"ShopCart"];
    [self.navigationController pushViewController:contactVC animated:YES];
}

@end