ios天气预报小记

github地址:https://github.com/macoooo/weather

UI

本次天气预报的总体就是呢,在scrollerView上自定义的UIView的tableView加在视图上,每次添加一个城市,加一个视图,更新数据然后就是传值操作,属性传过去,协议传回来 大概就是这样,一个scrollerView
ios天气预报小记_第1张图片
tableView再理解
没有注册cell,而是自定义直接写那种cell时,要将数据与创建视图分开,这样只创建一次,数据每次都更新。

if(indexPath.section == 3){
        UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"cell2"];
        if(cell == nil){
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

           _label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.bounds.size.width, 80)];
        }
            _label.text = _dataMuatbleArray[0][@"lifestyle"][0][@"txt"];
            _label.textColor = [UIColor whiteColor];
            _label.numberOfLines = 0;
            [cell.contentView addSubview:_label];

        cell.backgroundColor = [UIColor clearColor];
        return cell;
    }

如果手势与点击tableViewcell冲突

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        //判断如果点击的是tableView的cell,就把手势给关闭了
        return NO;//关闭手势
    }
    //否则手势存在
    return YES;
}

数据加载

网络请求数据get请求

- (void)creatpost
{
    NSString *urlString =[NSString stringWithFormat:@"https://free-api.heweather.com/s6/weather?location=%@&key=e1066ff38a7346128d608626c1b1477d", self.cityName];
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:urlString];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    NSURLSession *sharedSession = [NSURLSession sharedSession];

    NSURLSessionDataTask *dataTask = [sharedSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if(data){
            NSDictionary *firstDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
            self.dataMuatbleArray = [NSMutableArray array];
            self.dataMuatbleArray= firstDictionary[@"HeWeather6"];
            self->_oneDayDictionary = [[NSDictionary alloc] init];
            self->_oneDayDictionary = self->_dataMuatbleArray[0][@"daily_forecast"][0];
            NSLog(@"%@dddd",self.dataMuatbleArray[0][@"basic"][@"location"]);
            NSLog(@"%@",self->_dataMuatbleArray[0][@"daily_forecast"][0][@"wind_spd"]);
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [self.tableView reloadData];
            }];
        }
        else{
            NSLog(@"error = %@",error);
        }
    }];


    [dataTask resume];
}

对于网络请求的数据加在cell上,需要先请求再创建tableView,在加上去时还要判空,记得数组初始化

通过写set方法和init方法将数值传过来(在不是两个ViewController之间),z之前想不到这种,只会ViewConreoller之间传值,反思一下,还是oc没学好。。

在dissmiss回来后不会加载viewdidload,而在present过去是创建新的界面,会走viewdidload ,所以对于dissmiss回来之后,想更新scrollerview但是scrollerview在viewdidload中的问题,可以在协议传过来执行的那个方法 里重写一下scrollerview,即要想更新的东西

传值要想清楚是数组还是字符串

你可能感兴趣的:(ios天气预报小记)