天天看點

ios天氣預報小記

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

UI

本次天氣預報的總體就是呢,在scrollerView上自定義的UIView的tableView加在視圖上,每次添加一個城市,加一個視圖,更新資料然後就是傳值操作,屬性傳過去,協定傳回來 大概就是這樣,一個scrollerView

ios天氣預報小記

tableView再了解

沒有注冊cell,而是自定義直接寫那種cell時,要将資料與建立視圖分開,這樣隻建立一次,資料每次都更新。

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

           _label = [[UILabel alloc] initWithFrame:CGRectMake(, , self.bounds.size.width, )];
        }
            _label.text = _dataMuatbleArray[][@"lifestyle"][][@"txt"];
            _label.textColor = [UIColor whiteColor];
            _label.numberOfLines = ;
            [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[][@"daily_forecast"][];
            NSLog(@"%@dddd",self.dataMuatbleArray[][@"basic"][@"location"]);
            NSLog(@"%@",self->_dataMuatbleArray[][@"daily_forecast"][][@"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,即要想更新的東西

傳值要想清楚是數組還是字元串