๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/iOS

[iOS] ํ˜„์žฌ ์œ„์น˜ ๋‚ ์”จ & 24์‹œ๊ฐ„ & 7์ผ ๋ฐ์ดํ„ฐ ๋„์šฐ๊ธฐ - 2(Current Location Get weather 24 hours a day, 2 week)

by Fomagran ๐Ÿ’ป 2021. 2. 24.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!

 

์ €๋ฒˆ ์‹œ๊ฐ„์— ๋‚ ์”จ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๋Š” ๊ณผ์ •์„ ํ–ˆ์—ˆ๋Š”๋ฐ์š”.

 

(ํ˜น์‹œ ๋ชป๋ณด์‹  ๋ถ„๋“ค์€ ์—ฌ๊ธฐ ๋กœ ์ด๋™ํ•ด์„œ ๋จผ์ € ๋ณด๊ณ ์™€์ฃผ์„ธ์š”!)

 

์ด๋ฒˆ์—๋Š” ๋ฐ›์•„์˜จ ๋‚ ์”จ ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•ด์„œ ํ™”๋ฉด์— ๋„์›Œ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!


WeatherView

 

๋จผ์ € WeatherView๋กœ ์ด๋™ํ•ด์„œ ํ˜„์žฌ,24์‹œ๊ฐ„,์ฃผ๊ฐ„ ๋‚ ์”จ๋ฅผ ๋‹ด์„ ๋ณ€์ˆ˜๋“ค์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

   var currentWeather:CurrentWeather!
   var hourlyWeathers:[HourlyWeather] = []
   var weeklyWeathers:[WeeklyWeather] = []

๋จผ์ € ํ˜„์žฌ ๋‚ ์”จ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ ๋ ˆ์ด๋ธ”์— ๋งž๊ฒŒ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

  func setupCurrentWeather() {
        self.cityLabel.text = currentWeather.city
        self.dateLabel.text = "Today, \(currentWeather.date)"
        self.tempLabel.text = "\(currentWeather.temp)"
        self.weatherInfoLabel.text = currentWeather.weatherInfo
    }

๊ทธ ๋‹ค์Œ์œผ๋กœ ํ…Œ์ด๋ธ”๋ทฐ์— ํ…Œ์ด๋ธ”๋ทฐ์…€์„ ๋“ฑ๋กํ•ด์ฃผ์‹œ๊ณ  dataSource๋ฅผ self๋กœ ํ•ด์ค๋‹ˆ๋‹ค.

   func setupTableView() {
        weeklyTableView.register(UINib(nibName: "WeeklyTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "Cell")
        weeklyTableView.dataSource = self
    }

์ปฌ๋ ‰์…˜๋ทฐ๋„ ํ…Œ์ด๋ธ”๋ทฐ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

   func setupCollectionView(){
        hourlyCollectionView.register(UINib(nibName: "HourlyCollectionViewCell", bundle: Bundle.main), forCellWithReuseIdentifier: "Cell")
        hourlyCollectionView.dataSource = self
    }

HourlyCollectionViewCell

 

์ปฌ๋ ‰์…˜๋ทฐ์— ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๊ธฐ ์œ„ํ•ด์„œ ์ปฌ๋ ‰์…˜๋ทฐ์…€๋กœ ์ด๋™ํ•ด์ค๋‹ˆ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ ๋ ˆ์ด๋ธ”๊ณผ ์•„๋ฏธ์ง€์— ๋งž๊ฒŒ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

 

(์—ฌ๊ธฐ์„œ getWeatherIconFor๋Š” ์ €๋ฒˆ ์‹œ๊ฐ„์— Helpers์•ˆ์— ์•„์ด์ฝ˜์˜ ์ด๋ฆ„์„ ์ด๋ฏธ์ง€๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ ์šฉํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.)

class HourlyCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var tempLabel: UILabel!
    @IBOutlet weak var weatherImage: UIImageView!
    @IBOutlet weak var timeLabel: UILabel!
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    
    func generateCell(hourlyWeahter:HourlyWeather){
        tempLabel.text = "\(hourlyWeahter.temp)"
        timeLabel.text = hourlyWeahter.date
        weatherImage.image = getWeatherIconFor(hourlyWeahter.weatherIcon)
    }
}

WeeklyTableViewCell

 

ํ…Œ์ด๋ธ”๋ทฐ์…€๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์„ธํŒ…ํ•ด์ฃผ์„ธ์š”!

class WeeklyTableViewCell: UITableViewCell {

    @IBOutlet weak var weekLabel: UILabel!
    @IBOutlet weak var weatherImage: UIImageView!
    @IBOutlet weak var tempLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    func generateCell(weather:WeeklyWeather) {
        weekLabel.text = weather.date
        weatherImage.image = getWeatherIconFor(weather.weatherIcon)
        tempLabel.text = "\(weather.temp)"
    }
}

WeatherView

 

๋‹ค์‹œ WeatherView๋กœ ๋Œ์•„์™€์„œ

 

์•„๋ž˜์™€ ๊ฐ™์ด DataSource๋ฅผ extension์œผ๋กœ ํ™•์žฅํ•ด์„œ ํ…Œ์ด๋ธ”๋ทฐ ๋ฐ์ดํ„ฐ์†Œ์Šค๋ฅผ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

 

์œ„์—์„œ ๋งŒ๋“ค์–ด๋†“์€ generateCellํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ weeklyWeathers์— ๋‹ด๊ฒจ์žˆ๋Š” WeeklyWeather์„ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

extension WeatherView:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        weeklyWeathers.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! WeeklyTableViewCell
        cell.generateCell(weather: weeklyWeathers[indexPath.row])
        return cell
    }
}

์ฝœ๋ ‰์…˜๋ทฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

 

์ฝœ๋ ‰์…˜๋ทฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์œ„์—์„œ ๋งŒ๋“ค์–ด๋†“์€ generateCellํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ hourlyWeathers์— ๋‹ด๊ฒจ์žˆ๋Š” HourlyWeather์„ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

extension WeatherView:UICollectionViewDelegate,UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return hourlyWeathers.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as!
            HourlyCollectionViewCell
        cell.generateCell(hourlyWeahter: hourlyWeathers[indexPath.item])
        return cell
    }
}

์ด๋ ‡๊ฒŒ ๋ชจ๋“  ์ž‘์—…์„ ํ•˜๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ชจ๋“  ์ž‘์—…์„ ์‹คํ–‰ํ•  ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

  func setupAll() {
        setupCurrentWeather()
        setupCollectionView()
        setupTableView()
    }

 

์—ฌ๊ธฐ๊นŒ์ง€ WeatherView ์„ธํŒ…์ด ๋๋‚ฌ๊ตฌ์š”.

 

์ด์ œ๋ถ€ํ„ฐ๋Š” WeatherViewController์— WeatherView๋ฅผ ์„ธํŒ…ํ•˜๋Š”๋ฒ•์— ๋Œ€ํ•ด์„œ ๋‹ค๋ค„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


WeatherViewController

 

๊ฐ€์žฅ ๋จผ์ € WeatherView๋ฅผ ์„ธํŒ…ํ•ด์ค˜์•ผ๊ฒ ์ฃ ?

 

์•„๋ž˜์™€ ๊ฐ™์ด xib๋กœ ๋งŒ๋“  nib์„ WeatherView๋กœ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

weatherView์˜ ํ”„๋ ˆ์ž„์„ ํ˜„์žฌ ๋ทฐ์™€ ๋งž๊ฒŒ ์กฐ์ •ํ•ด์ฃผ์‹œ๊ตฌ์š”.

 

view์— addSubview๋กœ weatherView๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.(getCurrentWeather,getHourlyWeather,getWeeklyWeather์€ ์•„๋ž˜์„œ ์„ค๋ช…ํ• ๊ฒŒ์š”!)

    private func setupWeatherView() {
        let weatherView = Bundle.main.loadNibNamed("WeatherView", owner: self, options: nil)?.first as! WeatherView
        weatherView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
        getCurrentWeather(weatherView: weatherView)
        getHourlyWeather(weatherView: weatherView)
        getWeeklyWeather(weatherView: weatherView)
        self.view.addSubview(weatherView)
    }

 

๋จผ์ € ํ˜„์žฌ ๋‚ ์”จ๋ฅผ ์„ธํŒ…ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋จผ์ € WeatherView๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š” getCurrentWeather์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

์œ„์—์„œ ์šฐ๋ฆฌ๋Š” WeatherView์•ˆ์— currentWeather ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋†“์•˜์ฃ ?

 

์—ฌ๊ธฐ์„œ WeatherView์•ˆ์— currentWeather์„ ์ƒˆ๋กœ์šด CurrentWeather ์ธ์Šคํ„ด์Šค๋กœ ๋งŒ๋“ค์–ด์ค€ ๋’ค

 

CurrentWeather ์•ˆ์— ์žˆ๋Š” ๋ฉ”์†Œ๋“œ์ธ getCurrentWeather์„ ์‹คํ–‰์‹œ์ผœ์ค€ ๋’ค์— 

 

WeatherView์˜ setupAll์„ ํ•ด์ฃผ์–ด ๋ชจ๋“  ์„ธํŒ…์„ ํ•ด์ค๋‹ˆ๋‹ค.

  private func getCurrentWeather(weatherView:WeatherView) {
        weatherView.currentWeather = CurrentWeather()
        weatherView.currentWeather.getCurrentWeather {
            weatherView.setupAll()
        }
    }

24์‹œ๊ฐ„ ๋‚ ์”จ๋„ ํ˜„์žฌ ๋‚ ์”จ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์„ธํŒ…ํ•ด์ฃผ์ง€๋งŒ getWeeklyWeather์—์„œ ๋ฐ›์•„์˜จ weeklyWeathers๋ฅผ WeatherView ์•ˆ์— ์žˆ๋Š”

 

weeklyWeahters๋กœ ์„ค์ •ํ•ด์ค€ ๋’ค weatherView์˜ ์ปฌ๋ ‰์…˜๋ทฐ๋ฅผ reload์‹œ์ผœ์ค๋‹ˆ๋‹ค.

    private func getWeeklyWeather(weatherView:WeatherView) {
        WeeklyWeather.getWeeklyWeather { (weeklyWeathers) in
            weatherView.weeklyWeathers = weeklyWeathers
            weatherView.weeklyTableView.reloadData()
        }
    }

์ฃผ๊ฐ„ ๋‚ ์”จ๋„ 24์‹œ๊ฐ„ ๋‚ ์”จ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

   private func getHourlyWeather(weatherView:WeatherView) {
        HourlyWeather.getHourlyWeather { (hourlyWeathers) in
            weatherView.hourlyWeathers = hourlyWeathers
            weatherView.hourlyCollectionView.reloadData()
        }
    }

 

์ด๋ ‡๊ฒŒ ๋ชจ๋‘ ๋งˆ์นœ ํ›„ 

 

viewDidLoad์—  setupWeatherView ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œ์ผœ์ค๋‹ˆ๋‹ค.

 

(requestAuthorization ํ•จ์ˆ˜๋Š” ์—ฌ๊ธฐ ๊ธ€์„ ์ฐธ์กฐํ•ด์ฃผ์„ธ์š”!)

    override func viewDidLoad() {
        super.viewDidLoad()
        requestAuthorization()
        setupWeatherView()
    }

 

 


์•„๋ž˜์™€ ๊ฐ™์ด ํ˜„์žฌ ์œ„์น˜์— ์žˆ๋Š” ํ˜„์žฌ ๋‚ ์”จ์™€ ์•ž์œผ๋กœ์˜ 24์‹œ๊ฐ„๋™์•ˆ ๋‚ ์”จ ๊ทธ๋ฆฌ๊ณ  ์•ž์œผ๋กœ์˜ 2์ฃผ๊ฐ„์˜ ๋‚ ์”จ๊ฐ€ ๋„์›Œ์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!!!

 

(์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ๋กœ ์‹คํ–‰ํ•ด์„œ ์• ํ”Œ ๋ณธ์‚ฌ๊ฐ€ ์žˆ๋Š” ์œ„์น˜๋กœ ๋˜์–ด์žˆ๋„ค์š”... ใ…Žใ…Ž)

 

 

 

์ด๋ ‡๊ฒŒ 2ํŽธ์— ๊ฑธ์นœ ๋‚ ์”จ ์•Œ์•„๋ณด๊ธฐ๊ฐ€ ๋๋‚ฌ๋Š”๋ฐ์š”.

 

ํ˜น์‹œ๋ผ๋„ ๊ถ๊ธˆํ•œ ์ ์ด ์žˆ์œผ์‹œ๊ฑฐ๋‚˜ ์ง€์ ํ•˜์‹ค ๋ถ€๋ถ„์ด ์žˆ์œผ์‹œ๋ฉด ์–ธ์ œ๋“  ๋Œ“๊ธ€ ๋‹ฌ์•„์ฃผ์„ธ์š”!!

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€