我创建了一个包含4个单元格的收集视图。在其中之一中,我想显示一个MapView。这是我的收藏夹视图单元格代码:
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
}
答案 0 :(得分:0)
尝试以下代码:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100, height: 100)
}
别忘了实现委托方法。
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return [YOUR ITEM COUNT]
}