【问题标题】:tableview cellForRowAt indexPath values not coming outside to use in swift?tableview cellForRowAt indexPath 值没有在外面快速使用?
【发布时间】:2020-06-24 10:39:57
【问题描述】:

我正在获取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)
           }

【问题讨论】:

    标签: ios swift xcode uitableview variables


    【解决方案1】:

    您需要做的是从模型而不是单元格中获取值。在 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)
        }
       
    

    }

    【讨论】:

    • 单元格中有地址标签..和按钮..如果我点击按钮我需要显示从当前位置到该行坐标的方向..所以我在btnDirectTapped 中调用openMapForPlace
    • 建议:不要使用强制解包(userModel?.userAddresses![index]
    猜你喜欢
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多