我正在获取cellForRowAt indexPath
中的所有值,在这里根据其索引我需要一些值..我正在获取行索引,但正在获取该行值:
我在单元格中有一个按钮..因此,如果单击按钮,则需要该单元格的纬度和经度
在这里我需要cellForRowAt
中的openMapForPlace()
纬度和经度,但为什么不来? *
代码如下:
var latitude: Double!
var longitude: Double!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return userModel?.userAddresses.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: AddresCell = tableView.dequeueReusableCell(withIdentifier: "AddresCell", for: indexPath) as! AddresCell
let addr = userModel?.userAddresses![indexPath.row]
cell.delegate = self
cell.name.text = addr?.type
cell.typeAddLabel.text = addr?.addressName
let street = addr?.streetName
let colony = addr?.colony
let city = addr?.city
let pincode = addr?.pincode
let locality: String = addr?.buildingName ?? ""
let dorNum: String = addr?.houseNo ?? ""
let coord = addr?.location
latitude = coord?.latitude
longitude = coord?.longitude
cell.address.text = street! + "," + colony! + "," + city! + "," + pincode! + "," + locality + "," + dorNum
print("cellForRowAt indexPath \(indexPath.row) \(latitude), \(longitude)")
addressStr = street! + "," + colony! + "," + city! + "," + pincode! + "," + locality + "," + dorNum
cell.directions.tag = indexPath.row
return cell
}
func btnDirectTapped(cell: AddresCell) {
if let indexPath = self.addressTableview.indexPath(for: cell)
{
openMapForPlace()
print("selecte coordinates222 \(latitude!), \(longitude!)")
print("tableview row indexpath dir btn\(indexPath.row)")
print(indexPath.row)
}
}
func openMapForPlace() {
print("selecte coordinates1111 \(latitude!), \(longitude!)")
let regionDistance:CLLocationDistance = 10000
let coordinates = CLLocationCoordinate2DMake(12.9716, 77.5946)
let regionSpan = MKCoordinateRegion(center: coordinates, latitudinalMeters: regionDistance, longitudinalMeters: regionDistance)
let options = [
MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
]
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = "Place Name"
mapItem.openInMaps(launchOptions: options)
}
答案 0 :(得分:2)
您需要做的是从模型而不是单元格中获取值。在cellForRow中设置按钮标签,并获取标签以获取userAddresses
func openMapForPlace(index:Int) {
let addr = userModel?.userAddresses![index]
}
您可以像这样
func btnDirectTapped(cell: AddresCell) {
if let indexPath = self.addressTableview.indexPath(for: cell)
{
openMapForPlace(index: indexPath.row)
print("selecte coordinates222 \(latitude!), \(longitude!)")
print("tableview row indexpath dir btn\(indexPath.row)")
print(indexPath.row)
}
}