暑期第三周——天气预报

一、总体思路

    写这个项目用到了网络请求,通知传值,字典等知识。首先,我们要使用网络请求来完成模糊搜索城市,然后通过点击城市,可以跳转到对应的详情界面,详情界面中有搜索地区当前的天气、温度、湿度、体感温度、大气压强、能见度、运动指数等信息,还有一个scrollView和tableView分别显示着未来24小时的天气和温度以及未来七天的天气和温度。详情界面的导航控制器右侧有一个添加按钮,点击添加按钮就可以将当前地区添加到首页。首页也有一个tableView,这个tableView的cell显示着已添加的城市的名称和当天天气的背景图片,点击cell还可以跳转到该城市的详情页,如果是添加了多个城市,还可以通过左右滑动滚动视图来切换详情页。

二、功能详情

2.1 网络请求

    在写这个项目之前,必须要学会的一个点就是网络请求,网络请求的简单创建有一下五步:1. 创建URL; 2. 创建请求类; 3. 创建会话; 4. 根据会话创建任务; 5. 执行任务

代码示例:

#import <UIKit/UIKit.h>

//在开始网络请求前,要遵守NSURLSessionDelegate协议,该协议包含一些网络请求相关的函数
@interface ViewController : UIViewController<NSURLSessionDelegate, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource>

//在这里我们使用输入框来为网络请求项添加信息,并使用单元格来显示获取的信息
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UITableView *tabView;

//NSMutableData用于存储和修改json数据(它也可以存储视频、音频、图片等),将其初始化为一个空的数组或字典
@property (nonatomic, strong) NSMutableData *data;

//定义一个数组用于存放我们获取到的需要的数据
@property (nonatomic, strong) NSMutableArray *arrWeNeed;

@end
//首先先写一个创建网络请求的函数
//创建网络请求五大步骤:
//1. 创建URL请求地址
//2. 创建请求类
//3. 创建会话
//4. 根据会话创建任务
//5. 执行任务
- (void)creatURL {
    //该代码是根据我们的API的地址来创建一个URL,输入框中输入的内容就会被放入该地址,通过后面的服务器响应函数,我们就能通过该地址获得服务器返回的数据
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://geoapi.qweather.com/v2/city/lookup?location=%@&key=a7b9eca504f04d4d88216d70cc39ff69",self.textField.text]];
    //根据URL创建一个请求类
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //创建一个会话,其中NSURLSessionConfiguration用于配置NSSession对象的行为,这里传入的[NSOperationQueue mainQueue]是指协议方法在主线程中执行
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    //根据会话创建任务
    NSURLSessionTask *task = [session dataTaskWithRequest:request];
    //执行任务
    [task resume];
}

    创建好网络请求执行任务后,接下来会执行以下几个函数,当然要执行这些函数,前提是遵守了NSURLSessionDelegate协议:

    -(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler(接收服务器的响应)

-(void)URLSession:(NSURLSession *)session dataTask:( NSURLSessionDataTask *)dataTask didReceiveData:( NSData *)data  (接收到数据)

-(void)URLSession:(NSURLSession *)session task:( NSURLSessionTask *)task didCompleteWithError:( NSError *)error (数据请求完成或者请求出现错误调用的方法) 

代码示例:

//接收服务器响应
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
    //若data属性为空时为其创建实例对象,若不为空,则将其长度置为0来清除原数据,以便我们可以将新的响应数据存入该NSMutableData
    if (self.data == nil) {
        self.data = [[NSMutableData alloc] init];
    } else {
        self.data.length = 0;
    }
    
    //completionHandler(NSURLSessionResponseAllow)是一个函数参数,它的作用是在数据任务完成时通知系统我们可以接受响应结果
    //如果我们想要拒绝响应结果,就可以将NSURLSessionResponseReject作为参数传递给它
    completionHandler(NSURLSessionResponseAllow);
}

//接受到数据函数,该函数在每当我们接受到一次数据时就会调用一次
-(void)URLSession:(NSURLSession *)session dataTask:( NSURLSessionDataTask *)dataTask didReceiveData:( NSData *)data {
    //该函数名中的“data”就是我们接收到的数据
    //接收后,我们将该数据存入NSMutableData中,以便在后面的函数来解析数据
    [self.data appendData:data];
    
    //为arrWeNeed属性初始化
    self.arrWeNeed = [[NSMutableArray alloc] init];
}

//数据请求完成或者请求出错函数(重点)
//调用到该函数的时候,说明我们的网络请求已经获取到的数据,要么就是网络请求失败了,因此我们要对其判断是否失败,然后进行相应的操作
-(void)URLSession:(NSURLSession *)session task:( NSURLSessionTask *)task didCompleteWithError:( NSError *)error {
    //判断请求是否失败
    if (error == nil) {
        //创建一个字典来存储data中json数据编译后的数据
        //通过编译后的json数据会转化为一个字典,我们可以通过将URL中那个地址复制到浏览器上查看json数据,然后再将json数据赋值到json编译器中查看编译后的数据
        //编译后的数据是一个字典,它的key是“location”,value是若干个数组(每个数组的元素又是一个字典)。该数组中的数据就包含着我们所需要的数据,接下来我们来演示如何使用这些数据
        NSMutableDictionary *dictBianYi = [NSJSONSerialization JSONObjectWithData:self.data options:kNilOptions error:nil];
        
        //初始化一个数组,该数组用来获取上面字典中的value,该value是若干个数组(每个数组的元素又是一个字典)
        NSMutableArray *arrValue = [[NSMutableArray alloc] init];
        //获取名为location的键对应的数组,并将该数组赋给arrValue
        arrValue = dictBianYi[@"location"];
        
        //通过一个for循环,依次从获取到的数组中取出我们需要的值并放入arrWeNeed数组
        //arrWeNeed数组中的每个元素又是一个字典,因此我们可以在每个数组中通过相应的key取到它们的value,value就是我们最终要输出的值
        for (int i = 0; i < arrValue.count; i++) {
            NSMutableString *str = [NSMutableString stringWithFormat:@"%@ - %@", arrValue[i][@"name"], arrValue[i][@"adm1"]];
            [self.arrWeNeed addObject:str];
        }
    } else {
        NSLog(@"errol = %@", error);
    }
    
    [self.tabView reloadData];
}

        因为模糊搜索功能需要我们在输入的同时进行实时更新,因此我们将网络请求的创建放在- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;方法中(该方法需要遵守UITextFieldDelegate协议,它在输入框每次输入字符的时候都调用一次),以此来实现每输入一个字符的同时进行网络请求并且更新数据。

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //先设置一个输入框并设置代理
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, 374, 40)];
    self.textField.delegate = self;
    self.textField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.textField];
    
    self.tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 250, self.view.bounds.size.width, self.view.bounds.size.height - 300)];
    self.tabView.delegate = self;
    self.tabView.dataSource = self;
    [self.tabView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"id"];
    [self.view addSubview:self.tabView];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    [self creatURL];
    return YES;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cellNil = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mm"];
    for (int i = 0; i < self.arrWeNeed.count; i++) {
        if (indexPath.row == i) {
            UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"%d",i]];
            cell.textLabel.text = [NSString stringWithFormat:@"%@",self.arrWeNeed[i]];
            return cell;
        }
    }
    return cellNil;
}

@end

2.2 API与获取的数据        

        这里要首先对URL的创建中的API以及网络请求返回的数据详细说一下:

        想要获取一个API,我们首先需要在浏览器上找一个服务器,我使用的API是由和风天气开发服务提供的,链接:和风天气开发服务 ~ 强大、丰富的天气数据服务

        注册登录后,点击“项目管理”中的“创建项目”并创建一个项目,创建时选择Web API,创建好之后,点击KEY的查看,即可获取自己的key

        然后在首页开发文档中选择你需要的数据的API地址复制粘贴到你的代码中,并且为其添加好相应的参数就完成了。

        添加好参数后,我们可以将API地址输入到浏览器中,会得到一个页面,该页面上存放着我们获取到的json数据,我们需要将这些数据复制下来,粘贴到json编译器上,得到的是一个字典,根据信息的不同,字典key对应的value就是我们需要的数据,可能是一个字典,也可能是一个数组,数组中的每个元素是一个字典。在我们完成接收到数据的函数中,首先使用[NSJSONSerialization JSONObjectWithData:self.data options:kNilOptions error:nil];将json数据解析为字典,然后进行相应获取信息的操作。

        

 

2.3 详情界面

        我的详情界面由两个个scrollView和一个tableView组成。为了使跳转到该页面时先进行网络请求在加载UI,因此我将网络请求的创建写在了- (void)viewWillAppear:(BOOL)animated;中,并且对一些数组进行了初始化。进行完网络请求后,将获得的数据存放在属性的数组中。在完成网络请求的那个函数中写了一个- [[NSOperationQueue mainQueue] addOperationWithBlock:方法,该方法可以回到主线程,后接一个block块,其中写需要再进行的方法。在该block块中我们为需要显示在页面上的数据赋值

-(void)URLSession:(NSURLSession *)session task:( NSURLSessionTask *)task didCompleteWithError:( NSError *)error {
    if (error == nil) {
        if (task == self.dataTask2) {

            NSDictionary *dict2 = [NSJSONSerialization JSONObjectWithData:self.data2 options:kNilOptions error:nil];
            
            self.arr2 = [[NSMutableArray alloc] init];
            self.arr2 = dict2[@"daily"];
            for (int i = 0; i < self.arr2.count; i++) {
                NSArray *arrXiangQing = [NSArray arrayWithObjects:[NSString stringWithFormat:@"%@", self.arr2[i][@"iconDay"]], [NSString stringWithFormat:@"%@", self.arr2[i][@"tempMax"]], [NSString stringWithFormat:@"%@", self.arr2[i][@"tempMin"]], nil];
                [self.yuCeArray addObject:arrXiangQing];
            }
        }
        if (task == self.dataTask3) {
            NSDictionary *dict3 = [NSJSONSerialization JSONObjectWithData:self.data3 options:kNilOptions error:nil];
           
            NSMutableDictionary *arr3 = [[NSMutableDictionary alloc] init];
            
            arr3 = dict3[@"now"];
            
            self.xiangQingArray = [NSArray arrayWithObjects:[NSString stringWithFormat:@"%@", arr3[@"text"]], [NSString stringWithFormat:@"%@", arr3[@"temp"]], arr3[@"icon"], nil];
            self.shiDuStr = [NSString stringWithFormat:@"%@%%", arr3[@"humidity"]];
            self.fengDuStr = [NSString stringWithFormat:@"%@公里/小时", arr3[@"windSpeed"]];
            self.tiGanWenDuStr = [NSString stringWithFormat:@"%@", arr3[@"feelsLike"]];
            self.daQiYaQiangStr = [NSString stringWithFormat:@"%@百帕", arr3[@"pressure"]];
            self.nengJianDuStr = [NSString stringWithFormat:@"%@公里", arr3[@"vis"]];
            
        }
        if (task == self.dataTask4) {
            NSDictionary *dict4 = [NSJSONSerialization JSONObjectWithData:self.data4 options:kNilOptions error:nil];
            
            self.arr4 = [[NSMutableArray alloc] init];
            
            self.arr4 = dict4[@"hourly"];
            for (int i = 0; i < self.arr4.count; i++) {
                NSArray *arr = [NSArray arrayWithObjects: self.arr4[i][@"icon"], [NSString stringWithFormat:@"%@", self.arr4[i][@"temp"]], nil];
                [self.hourArray addObject:arr];
            }
        }
        if (task == self.dataTask5) {
            NSDictionary *dict5 = [NSJSONSerialization JSONObjectWithData:self.data5 options:kNilOptions error:nil];
            NSMutableArray *arr5 = [[NSMutableArray alloc] init];
            arr5 = dict5[@"daily"];
            self.yunDongStr = [NSString stringWithFormat:@"%@", arr5[0][@"level"]];
            self.pingJiaStr = [NSString stringWithFormat:@"%@", arr5[0][@"text"]];
        }
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            if (task == self.dataTask2) {
                self.yuBaoArray = [NSMutableArray arrayWithArray:self.yuCeArray];
                [self.tableView reloadData];
            }
            if (task == self.dataTask3) {
                self.weathLabel.text = [NSString stringWithFormat:@"%@", self.xiangQingArray[0]];
                self.wenDuLabel.text = [NSString stringWithFormat:@"%@", self.xiangQingArray[1]];
                
                UIImageView *imageView = [[UIImageView alloc] initWithFrame: self.view.bounds];
                imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"back%@.jpg", self.xiangQingArray[2]]];
                self.icon = self.xiangQingArray[2];
                [self.view insertSubview:imageView atIndex:0];
            }
            if (task == self.dataTask4) {
                
                for (int i = 0; i < 24; i++) {
                    UILabel *wenDu = [[UILabel alloc] initWithFrame:CGRectMake(40 + 120.5 * i, 160, 100, 40)];
                    wenDu.text = self.hourArray[i][1];
                    wenDu.textColor = [UIColor whiteColor];
                    [self.scroll addSubview:wenDu];
                    
                    UILabel *hourTime = [[UILabel alloc] initWithFrame:CGRectMake(40 + 120 * i, 10, 100, 40)];
                    NSDate *dateNow = [NSDate date];
                    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian];
                    unsigned unitFlags = NSCalendarUnitHour;
                    NSDateComponents *comp = [gregorian components: unitFlags fromDate: dateNow];
                    hourTime.text = [NSString stringWithFormat:@"%02ld时", (comp.hour + i + 1) % 24];
                    hourTime.textColor = [UIColor whiteColor];
                    
                    UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"weath%@.png", self.hourArray[i][0]]]];
                    imageV.frame = CGRectMake(20 + 120.5 * i, 65, 70, 70);
                    
                    [self.scroll addSubview:imageV];
                    [self.scroll addSubview: hourTime];
                    [self.tableView reloadData];
                }
            }
            if (task == self.dataTask5) {
                [self.tableView reloadData];
            }
        }];
        [self.tableView reloadData];
    } else {
        NSLog(@"error");
    }
}

2.4 添加城市后在滚动视图上展示详情

        该功能的实现我并不是传一个视图放到滚动视图上的,而是在搜索后的详情页面的- (void)viewWillDisappear:(BOOL)animated;函数(页面即将消失调用)中将该页面的所有数据打包为一个字典,然后通过通知传值将该字典传给首页,首页通过单元格的点击事件跳转到滚动视图的界面,在滚动视图上将传过来的字典的数据使用for循环按照相应的位置再次依次布局。不过这里我还在详情界面定义了一个BOOL类型的值来判断详情界面的消失是因为点击返回按钮消失的还是因为点击添加按钮消失的,如果是因为返回按钮消失的,就不会去传这个字典;如果是因为添加消失的,才会去传这个字典。

        关于通知传值,我会再另写博客详细说明。

//在详情界面
//将该字典传过去
- (void)viewWillDisappear:(BOOL)animated {
        NSLog(@"%@", self.hourArray);
        if (_quXiao == NO) {
            NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.yuCeArray, @"yuceArray", self.xiangQingArray, @"xiangQingArray", self.shiDuStr, @"shiduStr", self.fengDuStr, @"fengDuStr", self.tiGanWenDuStr, @"tiganWenDuStr", self.daQiYaQiangStr, @"daQiYaStr", self.nengJianDuStr, @"nengJianDuStr", self.hourArray, @"hourArray", self.yunDongStr, @"yunDongStr", self.pingJiaStr, @"pingJiaStr", nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"cityDict" object:nil userInfo:dictionary];
        }
    }
//滚动视图界面
//其中arrDictArray就是我们传过来的字典
#import "gunDongViewController.h"
#import "WeathViewController.h"
#import "WeathTableViewCell.h"
#import "OtherTableViewCell.h"

@interface gunDongViewController ()


@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic) NSMutableArray *nextTimeArray;
@property (nonatomic, strong) UIPageControl *pageControl;

@end

@implementation gunDongViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * self.arrDictArray.count, self.view.bounds.size.height);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    
    [self.scrollView setContentOffset:CGPointMake(self.view.bounds.size.width * self.location, 0)];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"menu.png"] forState:UIControlStateNormal];
    button.frame = CGRectMake(self.view.bounds.size.width - 80, self.view.bounds.size.height - 70, 50, 50);
    [button addTarget:self action:@selector(pressBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100, self.view.bounds.size.height -
    50, 200, 20)];
    self.pageControl.numberOfPages = self.cityArray.count;
    self.pageControl.currentPage = self.location;
    self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
    self.pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    [self.view addSubview:self.pageControl];
    
    [self.pageControl bringSubviewToFront: button];
    
    NSLog(@"%@", self.arrDictArray);
    
    for (int i = 0; i < self.arrDictArray.count; i++) {
        UIScrollView *BigScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width * i, 0, self.view.bounds.size.width, self.view.bounds.size.height - 80)];
        BigScrollView.contentSize = CGSizeMake(self.view.bounds.size.width, 1200);
        [self.scrollView addSubview:BigScrollView];
        
        UILabel *cityNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, self.view.bounds.size.width, 60)];
        cityNameLabel.text = self.cityArray[i];
        cityNameLabel.textColor = [UIColor whiteColor];
        cityNameLabel.numberOfLines = 1;
        cityNameLabel.textAlignment = NSTextAlignmentCenter;
        cityNameLabel.font = [UIFont systemFontOfSize:42];
        [BigScrollView addSubview:cityNameLabel];
        
        UILabel *weathLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, self.view.bounds.size.width, 40)];
        weathLabel.text = self.arrDictArray[i][@"xiangQingArray"][0];
        weathLabel.textColor = [UIColor whiteColor];
        weathLabel.font = [UIFont systemFontOfSize:18];
        weathLabel.textAlignment = NSTextAlignmentCenter;
        [BigScrollView addSubview:weathLabel];
        
        UILabel *wenDuLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 130, self.view.bounds.size.width, 80)];
        wenDuLabel.text = self.arrDictArray[i][@"xiangQingArray"][1];
        wenDuLabel.textColor = [UIColor whiteColor];
        wenDuLabel.font = [UIFont systemFontOfSize:72];
        wenDuLabel.textAlignment = NSTextAlignmentCenter;
        [BigScrollView addSubview:wenDuLabel];
        
        UIScrollView *hengXiangScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 270, self.view.bounds.size.width, 200)];
        hengXiangScroll.contentSize = CGSizeMake(120 * 24 + 20, 200);
        [BigScrollView addSubview:hengXiangScroll];
        
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 480, self.view.bounds.size.width, 750) style:UITableViewStylePlain];
        tableView.tag = i;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.scrollEnabled = NO;
        tableView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.4 alpha:0.3];
        [tableView registerClass:[WeathTableViewCell class] forCellReuseIdentifier:@"id"];
        [BigScrollView addSubview:tableView];
        
        for (int j = 0; j < 24; j++) {
            UILabel *wenDu = [[UILabel alloc] initWithFrame:CGRectMake(40 + 120.5 * j, 160, 100, 40)];
            wenDu.text = self.arrDictArray[i][@"hourArray"][j][1];
            wenDu.textColor = [UIColor whiteColor];
            [hengXiangScroll addSubview:wenDu];
            
            UILabel *hourTime = [[UILabel alloc] initWithFrame:CGRectMake(40 + 120 * j, 10, 100, 40)];
            NSDate *dateNow = [NSDate date];
            NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian];
            unsigned unitFlags = NSCalendarUnitHour;
            NSDateComponents *comp = [gregorian components: unitFlags fromDate: dateNow];
            hourTime.text = [NSString stringWithFormat:@"%02ld时", (comp.hour + j + 1) % 24];
            hourTime.textColor = [UIColor whiteColor];
            
            UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"weath%@.png", self.arrDictArray[i][@"hourArray"][j][0]]]];
            imageV.frame = CGRectMake(20 + 120.5 * j, 65, 70, 70);
            
            [hengXiangScroll addSubview:imageV];
            [hengXiangScroll addSubview: hourTime];
        }
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width * i, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"back%@.jpg", self.arrDictArray[i][@"xiangQingArray"][2]]];
        [self.scrollView insertSubview:imageView atIndex:0];
    }
    self.nextTimeArray = [[NSMutableArray alloc] init];
    for (int i = 1; i <= 7; i++) {
        NSDate *dataNext = [[NSDate alloc] initWithTimeIntervalSinceNow:3600 * 24 * i];
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian];
        unsigned unitFlags = NSCalendarUnitWeekday;
        NSDateComponents *comp = [gregorian components: unitFlags fromDate: dataNext];
        [self.nextTimeArray addObject:[NSString stringWithFormat:@"%@", [self xingQi:comp.weekday]]];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSInteger pageX = self.scrollView.contentOffset.x / self.view.bounds.size.width;
    self.pageControl.currentPage = pageX;
}

- (void) pressBack {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSString*) xingQi:(NSInteger) weekday {
    if (weekday == 1) {
        return @"星期日";
    }
    if (weekday == 2) {
        return @"星期一";
    }
    if (weekday == 3) {
        return @"星期二";
    }
    if (weekday == 4) {
        return @"星期三";
    }
    if (weekday == 5) {
        return @"星期四";
    }
    if (weekday == 6) {
        return @"星期五";
    }
    if (weekday == 7) {
        return @"星期六";
    }
    return nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 11;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < 7) {
        return 50;
    }
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cellNil = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"g"];
    for (int j = 0; j < self.arrDictArray.count; j++) {
        if (tableView.tag == j) {
            for (int i = 0; i < 7; i++) {
                if (indexPath.row == i) {
                    WeathTableViewCell *cell = [[WeathTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"%d",i]];
                    cell.wenDuHeighLabel.text = self.arrDictArray[j][@"yuceArray"][i][1];
                    cell.wenDuLowLabel.text = self.arrDictArray[j][@"yuceArray"][i][2];
                    cell.timeLabel.text = self.nextTimeArray[i];
                    return cell;
                }
            }
            if (indexPath.row == 7) {
                UITableViewCell *cellPingLun = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"pingLun"];
                cellPingLun.backgroundColor = [UIColor clearColor];
                cellPingLun.textLabel.text = self.arrDictArray[j][@"pingJiaStr"];
                cellPingLun.textLabel.numberOfLines = 2;
                cellPingLun.textLabel.textColor = [UIColor whiteColor];
                return cellPingLun;
            }
            if (indexPath.row == 8) {
                OtherTableViewCell *cell1 = [[OtherTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell1"];
                cell1.biaoTi1Label.text = @"湿度";
                cell1.biaoTi2Label.text = @"风速";
                cell1.neiRong1Label.text = self.arrDictArray[j][@"shiduStr"];
                cell1.neiRong2Label.text = self.arrDictArray[j][@"fengDuStr"];
                return cell1;
            }
            if (indexPath.row == 9) {
                OtherTableViewCell *cell2 = [[OtherTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell2"];
                cell2.biaoTi1Label.text = @"体感温度";
                cell2.biaoTi2Label.text = @"气压";
                cell2.neiRong1Label.text = self.arrDictArray[j][@"tiganWenDuStr"];
                cell2.neiRong2Label.text = self.arrDictArray[j][@"daQiYaStr"];
                NSLog(@"%@",cell2.neiRong1Label.text);
                return cell2;
            }
            if (indexPath.row == 10) {
                OtherTableViewCell *cell3 = [[OtherTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell3"];
                cell3.biaoTi1Label.text = @"能见度";
                cell3.biaoTi2Label.text = @"运动指数";
                cell3.neiRong1Label.text = self.arrDictArray[j][@"nengJianDuStr"];
                cell3.neiRong2Label.text = self.arrDictArray[j][@"yunDongStr"];
                return cell3;
            }
        }
    }
    return cellNil;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值