tableview cellForRowAt indexPath 值没有在外面快速使用?

Posted

技术标签:

【中文标题】tableview cellForRowAt indexPath 值没有在外面快速使用?【英文标题】:tableview cellForRowAt indexPath values not coming outside to use in swift? 【发布时间】: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)
           

【问题讨论】:

【参考方案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]

以上是关于tableview cellForRowAt indexPath 值没有在外面快速使用?的主要内容,如果未能解决你的问题,请参考以下文章

简化 TableView 中的 cellForRowAt

无法将“Int”类型的值分配给“String”类型?在 (cellForRowAt) 中使用 Tableview

如何从 cellForRowAt 调用 draw(_ rect: CGRect) 在自定义 tableview 上绘图?

在 tableView(_ tableView:, cellForRowAt indexPath:) 委托方法中使用调度组; “未使用 UITableViewCell 类型的表达式”

tableview cellForRowAt indexPath 值没有在外面快速使用?

根据索引为 tableview 单元格按钮提供不同的颜色。这在 WilldisplayCell 和 CellForROwAt 级别都不起作用