puuurm / swift-weatherforecast

iOS app
1 stars 0 forks source link

Dynamically create a marker image view only in the Table Section showing the User Location #31

Closed puuurm closed 6 years ago

puuurm commented 6 years ago
struct WeatherTableCellViewModel {
    var timeString: String
    var cityString: String
    var temperatureString: String
    var weatherDetail: WeatherDetail
    var isUserLocation: Bool // + 해당 셀이 현재 위치 정보를 담고 있는지에 대한 여부 
}

func setContents(viewModel: WeatherTableCellViewModel) {
    cityLabel.text = viewModel.cityString
    temperature.text = viewModel.temperatureString
    timeLabel.text = viewModel.timeString
    // 해당 셀이 사용자 위치에 대한 정보를 담고 있고 마커 라벨이 없다면 마커를 생성하라.
    if viewModel.isUserLocation && !hasMarkerLabel {
        createMarker()
    }
}

private func createMarker() {
    let markerImageView = UIImageView(image: UIImage(named: "marker"))
    addSubview(markerImageView)
    markerImageView.contentMode = .scaleAspectFit
    markerImageView.translatesAutoresizingMaskIntoConstraints = false
    markerImageView.heightAnchor.constraint(equalTo: cityLabel.heightAnchor).isActive = true
    markerImageView.trailingAnchor.constraint(equalTo: cityLabel.leadingAnchor, constant: -5).isActive = true
    markerImageView.topAnchor.constraint(equalTo: cityLabel.topAnchor).isActive = true
    hasMarkerLabel = true 
}