ios - Collection View 单元格内的 MKMapview 不可见

标签 ios swift uicollectionview mapkit

我创建了一个包含 4 个单元格的 Collection View 。在其中一个我想显示一个 map View 。这是我的 Collection View 单元格代码:

import Foundation
import UIKit
import MapKit
import CoreLocation

class PestañaUno: UICollectionViewCell, CLLocationManagerDelegate, MKMapViewDelegate
{
    let manager = CLLocationManager()

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

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

    var Mapa: MKMapView =
    {
        var map = MKMapView()
        map.frame = CGRect(x: 0, y: 0, width: 300, height: 400)
        map.mapType = MKMapType.standard
        return map
    }()

    func Setup()
    {
        addSubview(Mapa)
    }

这是当我尝试调用该 collectionviewcell 代码时。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    if indexPath.item == 1
    {
        let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "PestañaUno", for: indexPath) as! PestañaUno
    }

    return myCell
}

最佳答案

尝试以下代码:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 100, height: 100)
}

不要忘记实现委托(delegate)方法。

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return [YOUR ITEM COUNT]
}

关于ios - Collection View 单元格内的 MKMapview 不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52150509/

相关文章:

ios - 带有自定义布局的 UICollectionView 变为空白

swift - 在 SwiftUI 中显示行/分隔符 View

Swift + CoreLocation 不工作

ios - 通用类不将委托(delegate)调用转发给具体子类

iphone - objective-c 源 locationManager 在自己的类中

ios - UINavigationController 在设备旋转时自动弹出到 Root View

ios - UICollectionView 不从 dispatch_async 刷新

swift - UICollectionView 不显示图像

iphone - UITablViewCell 上的 MKMapView 崩溃

ios - 为什么在代码中选择单元格后我的自定义 Collection View 单元格意外为零?