gpt4 book ai didi

ios - 使用索引在annotationView中显示文本数组

转载 作者:行者123 更新时间:2023-11-30 12:03:31 25 4
gpt4 key购买 nike

我无法显示变量中的所有地址

var allAddress: [Address] = []

在这个变量中,我从firebase获取少量地址,并且我想在我的annotationView中显示所有地址,但是当我尝试显示所有地址,我在所有 annotationView 中只看到一个地址,但如果我使用 for..in.. index 进行打印,我会看到 index0>、index1index2index3 以及其他...以及如果我打印此:

print("address - \(allAddress[index].address)") 

我得到了 firebase 中的所有地址,总共 6

它打印我的索引allAddress[index].address

index 1
address - Москва, ул. Правды д.24, строение 3
index 2
address - Москва, ул.Электрозаводская д.21
index 3
address - Москва, ул.Бутырская д.8
index 4
address - Москва, 2-Я Звенигородская улица 12 строение 21
index 5
address - Москва, Николоямская 52, стр. 1

这是我的代码:

let centerInfo = UILabel(frame: CGRect(x: 0, y: 0, width: 150, height: 100))
for index in 0..<allAddress.count {
print("index \(index)")
centerInfo.text = allAddress[index].address
centerInfo.numberOfLines = 0
centerInfo.lineBreakMode = .byWordWrapping

print("address - \(allAddress[index].address)")
}
annotationView?.detailCalloutAccessoryView = centerInfo

如何显示我的 centerInfo.text 中的所有地址?

请检查.gif,它在所有annotationView上显示相同的地址

enter image description here

附注在annotation.title中不需要使用,我不适合

已更新。所有代码:

class AllAddressMapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var mapView: MKMapView!

var allAddress: [Address] = []

var studioRef: DatabaseReference!

override func viewDidLoad() {
super.viewDidLoad()

studioRef = Database.database().reference(withPath: "Photo1")

studioRef.observe(.value, with: { (snapshot) in

for imageSnap in snapshot.children {

let studioObj = Studio(snapshot: imageSnap as! DataSnapshot)

self.allAddress.append(studioObj)

for index in 0..<self.allAddress.count {

let geocoder = CLGeocoder()

geocoder.geocodeAddressString(self.allAddress[index].address, completionHandler: { (placemarks, error) in

guard error == nil else { return }

guard let placemarks = placemarks else { return }

if let placemark = placemarks.first {

let annotation = MKPointAnnotation()

guard let address = placemark.location else { return }
annotation.coordinate = address.coordinate

self.mapView.addAnnotation(annotation)

}

geocoder.cancelGeocode()

})

}

}

})

mapView.delegate = self
mapView.mapType = .standard
mapView.isZoomEnabled = true
mapView.isScrollEnabled = true

}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

guard !(annotation is MKUserLocation) else { return nil }

let annotationID = "PinMap"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationID) as? MKPinAnnotationView

if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationID)
annotationView?.canShowCallout = true
} else {
annotationView?.annotation = annotation
}

let leftImageNavigationButton = UIImage(named: "auto")
let tintedColorleftImageNavigationButton = leftImageNavigationButton?.withRenderingMode(.alwaysTemplate)
let leftNavigationButton = UIButton(type: .custom)
leftNavigationButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
leftNavigationButton.setImage(tintedColorleftImageNavigationButton, for: UIControlState())
leftNavigationButton.tintColor = #colorLiteral(red: 0, green: 0.4784313725, blue: 1, alpha: 1)
annotationView?.leftCalloutAccessoryView = leftNavigationButton

let rightButtonInfo = UIButton(type: .detailDisclosure)
annotationView?.rightCalloutAccessoryView = rightButtonInfo

let centerInfo = UILabel(frame: CGRect(x: 0, y: 0, width: 150, height: 100))
for index in 0..<allAddress.count {
print("index \(index)")
centerInfo.text = allAddress[index].address
centerInfo.numberOfLines = 0
centerInfo.lineBreakMode = .byWordWrapping

print("address - \(allAddress[index].address)")
}
annotationView?.detailCalloutAccessoryView = centerInfo

annotationView?.calloutOffset = CGPoint(x: -8, y: 0)

return annotationView

}

}

最佳答案

从您的代码来看,您正在使用相同的 UILabel对于所有注释和循环,您将设置 text相同的属性UILabel每次。这就是为什么它显示最后一个 address所有注释。

尝试分配一个单独的 UILabel对于每个注释并设置 text属性到指定address

关于ios - 使用索引在annotationView中显示文本数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46926416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com