我创建了一个包含 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/