ios - 在 UITableView 上出队和重新排队时,MKMapView 不断缩小

标签 ios swift uitableview mkmapview

我正在构建一个应用程序,该应用程序具有一个 UITableController 和一个自定义 UITableViewCell,其中包含一个带有一些文本的 MKMapView。我的内容显示正确,但是当单元格出队或从 View 中删除时,一旦它重新出现, map 就会稍微缩小,并且随着您出队和重新排队的次数越来越多,它会不断缩小。有人可以帮我从这里出去吗?我已进入 Storyboard编辑器并关闭了 map View 的所有缩放和滚动,我不确定为什么 View 继续缩小。

TableViewController.swift:

....
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MapTableViewCell
    cell.mapView.delegate = self
    showRoute(routes: locationStructArray[indexPath.row].route, color: UIColor.green, mapView: cell.mapView)

    return cell
}

func showRoute(routes:[MKRoute], color: UIColor, mapView: MKMapView){
    for i in 0..<routes.count{
        plotPolyLine(route: routes[i], color: color, mapView: mapView)
    }
}

func plotPolyLine(route: MKRoute, color: UIColor, mapView: MKMapView){
    let poly = route.polyline
    let mypoly = MyColoredPolyline(points: poly.points(), count: poly.pointCount)
    mypoly.color = color
    mapView.add(mypoly)
    if mapView.overlays.count == 1 {
        mapView.setVisibleMapRect(mypoly.boundingMapRect, edgePadding: UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0), animated: false)
    }
    else {
        let polylineBoundingRect =  MKMapRectUnion(mapView.visibleMapRect, mypoly.boundingMapRect)
        mapView.setVisibleMapRect(polylineBoundingRect, edgePadding: UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0), animated: false)
    }
}

extension TableViewController: MKMapViewDelegate {
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        let route: MyColoredPolyline = overlay as! MyColoredPolyline

        let pr = MKPolylineRenderer(polyline: route)
        pr.lineWidth = 3.0
        pr.strokeColor = route.color
        return pr
    }
}

编辑 GIF,其中添加了建议:http://gph.is/2qAx6Ls

最佳答案

cell.mapView.removeOverlays(cell.mapView.overlays)

我认为这是因为您忘记从要重复使用的同一 map View 中删除旧的折线:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let cell = ...
  cell.mapView.delegate = self
  cell.mapView.removeOverlays(cell.mapView.overlays)
  showRoute(routes: locationStructArray[indexPath.row].route, color: UIColor.green, mapView: cell.mapView)
  return cell
}

更好的是在 MapTableViewCellprepareForReuse() 中执行此操作方法替代:

class MapTableViewCell: UITableViewCell {
  ...

  override func prepareForReuse() {
    super.prepareForReuse()
    mapView.removeOverlays(mapView.overlays)
  }
}

关于ios - 在 UITableView 上出队和重新排队时,MKMapView 不断缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44080333/

相关文章:

iphone - 将对象NSMutableArray添加到NSDictionary

ios - Swift 中的函数式图形

IOS Swift Core Data 如何在 propertiesToFetch 中添加不在 propertiesToGroupBy 中的字段

带有类型化的 Swift 协议(protocol)。协议(protocol)只能用作通用约束

ios - iOS 5 中的 scrollviewTexturedBackgroundColor 是否发生了变化?

swift - 如何声明具有两种可能类型的变量

ios 在 UITableView 标题 View 中添加一个 uicollectionView

ios - UISearchController出现在NavigationBar后面

ios - 带有自定义 UIEdgeInsets 的 UILabel 在 UITableViewCell 中截断

ios - UIScreen 是否有屏幕打开通知?