0

私は "タブ"の経験をしようとしています。各タブは画面の幅を持ち、ユーザーはそれぞれのタブをスワイプできます。インデックス3のMKMapViewが実装されているタブを除いて、すべてうまく動作しています。コレクションビューでMKMapViewを事前にロード

enter image description here

ユーザーオープンアプリ、コレクションビューがIndexPath(row: 1, section: 0)

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
{ 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PageIndentifier, for: indexPath) as! TabCell 

    if (indexPath.item == 0) 
    { 
     cell.SetupNearestView() 
     NearestView = cell.nearestView 
     NearestView?.Delegate = self 
    } 

    if (indexPath.item == 1) 
    { 
     cell.SetupStationsView() 
     StationsView = cell.stationsView 
     StationsView?.Delegate = self 
    } 

    if (indexPath.item == 2) 
    { 
     cell.SetupMapView() 
     MapView = cell.stationsMapView 
    } 

    return cell 
} 

問題のためにインデックスに「1」、またcellForIndexAtIndexPathが呼び出される初期化されるとUIが約二時に、ユーザースワイプのためにブロックされていることです1 - > 2の場合、コレクションビューのトリガーcellForIndexAtIndexPathIndexPath(row: 2, section: 0)のために呼び出されます。私はそのインデックスで "強制的に"ロードセルを試していましたが、不幸にも地図は画面上にレンダリングする直前に初期化されているようです。そのための回避策はありますか?

編集:あなたはMapViewのセルを再利用していないので

TabCell.swift

class TabCell : UICollectionViewCell 
{ 
var nearestView : NearestView? 
var stationsView : StationsView? 
var stationsMapView : MapView? 

override init(frame: CGRect) 
{ 
    super.init(frame: frame) 

    let gradient = CAGradientLayer() 
    gradient.frame = bounds 
    gradient.colors = [UIColor.clear, RuntimeResources.Get(color: .VeryDarkBlue).cgColor] 
    layer.insertSublayer(gradient, at: 0) 
} 

required init?(coder aDecoder: NSCoder) 
{ 
    fatalError("init(coder:) has not been implemented") 
} 

func SetupNearestView() 
{ 
    if (nearestView != nil) 
    { 
     return 
    } 

    nearestView = NearestView(frame: CGRect(x: 0, y: 0, width: frame.width, height: frame.height)) 
    addSubview(nearestView!) 

    addConstraintsWithFormat(format: "H:|[v0]|", views: nearestView!) 
    addConstraintsWithFormat(format: "V:|[v0]|", views: nearestView!) 

} 

func SetupMapView() 
{ 
    if (stationsMapView != nil) 
    { 
     return 
    } 

    stationsMapView = MapView(frame: CGRect(x: 0, y: 0, width: frame.width, height: frame.height)) 
    addSubview(stationsMapView!) 

    addConstraintsWithFormat(format: "H:|[v0]|", views: stationsMapView!) 
    addConstraintsWithFormat(format: "V:|[v0]|", views: stationsMapView!) 
} 

func SetupStationsView() 
{ 
    if (stationsView != nil) 
    { 
     return 
    } 

    stationsView = StationsView(frame: CGRect(x: 0, y: 0, width: frame.width, height: frame.height)) 
    addSubview(stationsView!) 

    addConstraintsWithFormat(format: "H:|[v0]|", views: stationsView!) 
    addConstraintsWithFormat(format: "V:|[v0]|", views: stationsView!) 
} 
} 

MapView.swift

import UIKit 
import MapKit 

class MapView: MKMapView, MKMapViewDelegate 
{ 
private let infoViewHeight = CGFloat(500) 
private let FocusZoomLevel = 0.01 
private let ZoomLevel = 0.04 
private let centerOfSzczecin = CLLocationCoordinate2D(latitude: 53.433345, longitude: 14.544139) 
private var infoViewTopConstraint : NSLayoutConstraint? 

lazy var mainStationView : UIView = { 

    let msv = UIView(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: self.infoViewHeight)) 
    msv.translatesAutoresizingMaskIntoConstraints = false 
    msv.backgroundColor = RuntimeResources.Get(color: .DarkBlue) 
    return msv 
}() 

override init(frame: CGRect) 
{ 
    super.init(frame: frame) 
    delegate = self 

    // Adding info view 
    SetupInfoView() 

    // Setting center of map 
    let span = MKCoordinateSpanMake(ZoomLevel, ZoomLevel) 
    let region = MKCoordinateRegion(center: centerOfSzczecin, span: span) 
    setRegion(region, animated: false) 
} 

required init?(coder aDecoder: NSCoder) 
{ 
    fatalError("init(coder:) has not been implemented") 
} 

private func SetupInfoView() 
{ 
    addSubview(mainStationView) 
    infoViewTopConstraint = mainStationView.topAnchor.constraint(equalTo: self.bottomAnchor) 
    infoViewTopConstraint?.isActive = true 
    mainStationView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true 
    mainStationView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true 
    mainStationView.heightAnchor.constraint(equalToConstant: infoViewHeight).isActive = true 
} 

} 
+0

あなたのUIがブロックされている場合は、ここで何か間違っています:cell.SetupMapView()は、あらかじめロードすることで何もしません。 –

+0

私はこれについては分かりません。私は新しいMapViewインスタンスを作成し、これの制約を操作しています。余分な作業はありません。データのロード、注釈の描画、CPUの集中またはブロックのタスクはありません。 – Marcin

+0

まず、問題を簡単に確認し、質問に呼び出すコードを追加することができます。次に、cellForRowAtItemが呼び出されるたびにcell.SetupMapView()関数を呼び出しています。セルが呼び出されるたびにすべてをやり直すのを避けるためにチェックを行いますか? –

答えて

0

はあなたUICollectionViewCell(あなたUIViewController中)の外の変数としてあなたのMapViewをロードしてみてくださいcollectionView:willDisplayCell:forItemAtIndexPath:

にセルへのサブビューとして追加しcollectionView:didEndDisplayingCell:forItemAtIndexPath:にあなたがセルから削除することができます。

あなたのセルの外にあるすべてのmapViewコードをロードし、Cellを使用して地図をサブビューとして表示するだけです。

+1

あなたは正しいです!マップのオブジェクトを初期化するのは実際にはCPUを大量に使いますが、親ViewControllerの内部でこれを初期化し、サブビューとしてセルに追加しました。ありがとう! – Marcin

+0

@Marcin喜んで:) –

関連する問題