UITableView contentSize 在 ios7 中不会更新



我有一个UITableViewController,它通常使用数据源委托方法加载一些单元格。

在ios8中,一切都很好,但在ios7中,contentsize的高度似乎没有达到单元格的正确高度。

它在底部正确地添加了离开屏幕的单元格,当我想向下滚动时,它不会滚动,它只是像在底部一样开始反弹动画。当我拖动时,我可以看到屏幕底部下面的单元格,但当我释放它时,它会反弹回来。

我想这只是我丢失的一些财产,但我似乎找不到是哪一件。

我根本没有配置表视图,我只是使用数据源向其中添加单元格

编辑

我正在使用自动布局。

当我打印出contentsize时,它会打印出CGSizeZero

var BookingInfo:[[String:AnyObject]]? { didSet { self.tableView.reloadData() } }
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return BookingInfo == nil ? 0 : BookingInfo!.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let bookingInfo = BookingInfo![indexPath.row]
    let id = (bookingInfo["id"] as Int).toString()
    var cell = tableView.dequeueReusableCellWithIdentifier(id) as? ListCell
    if cell == nil { cell = ListCell(bookingInfo: bookingInfo, reuseID: id) }
    return cell!
}

BookingInfo是在此视图之前的另一个视图中的prepareSegue函数中设置的。

ListCell是一个自定义单元格,它使用BookingInfo中的坐标加载地图。

我正在更改单元格ID,以便在排序时快速访问,这样地图就不必重新加载。

编辑2

我正在使用MapBox

class ListCell:UITableViewCell, RMMapViewDelegate {
    var BookingInfo:[String:AnyObject]!
    var mapView:RMStaticMapView!

    func mapView(mapView: RMMapView!, layerForAnnotation annotation: RMAnnotation!) -> RMMapLayer! {
        let marker = RMMarker(UIImage: UIImage(named: "MapPin.png")!.scaledImage(0.66))
        marker.anchorPoint = CGPoint(x: 0.5, y: 1)
        annotation.layer = marker
        marker.canShowCallout = false
        return marker
    }
    func setup(frame: CGRect) {
    self.backgroundColor = nil
        let mapFrame = CGRect(x: 0, y: 0, width: frame.height, height: frame.height)
        if mapView == nil {
        self.mapView = RMStaticMapView(frame: mapFrame, mapID: "an ID")
            if self.mapView != nil {
                self.mapView!.delegate = self
                self.mapView!.showLogoBug = false
                self.mapView!.setZoom(11, atCoordinate: CLLocationCoordinate2D(latitude: (self.BookingInfo["lat"] as String).toDouble(), longitude: (self.BookingInfo["lon"] as String).toDouble()), animated: false)
                self.mapView!.addAnnotation(RMAnnotation(mapView: self.mapView, coordinate: self.mapView!.centerCoordinate, andTitle: ""))
                self.contentView.addSubview(self.mapView!)
            }
        }
    }
    override func layoutSubviews() {
        gcd.async(.Main) {
            self.setup(self.bounds)
        }
        super.layoutSubviews()
    }
    convenience init(bookingInfo: [String:AnyObject], reuseID: String) {
        self.init(style: .Default, reuseIdentifier: reuseID)
        self.BookingInfo = bookingInfo
    }
}

这个类中还有更多,但它们与地图无关,它们只是简单的UILabels,删除了有问题的标签,所以不会太长。

在@ROB的回答后编辑

我添加了约束,但内容大小仍然为{0,0}

// In viewDidLoad
self.tableView.rowHeight = 100   
// In ListCell.setup:
self.mapView = RMStaticMapView(frame: mapFrame, mapID: "an ID")
self.mapView.setTranslatesAutoresizingMaskIntoConstraints(false)    
self.contentView.addSubview(self.mapView!)
var arr = NSLayoutConstraint.constraintsWithVisualFormat("|[mapView]", options: nil, metrics: nil, views: ["mapView":self.mapView])
arr += NSLayoutConstraint.constraintsWithVisualFormat("V:|[mapView]|", options: nil, metrics: nil, views: ["mapView":self.mapView])
self.contentView.addConstraints(arr)
let constraintRatio = NSLayoutConstraint(item: self.mapView, attribute: .Width, relatedBy: .Equal, toItem: self.mapView, attribute: .Height, multiplier: 1, constant: 0)
self.mapView.addConstraint(constraintRatio)

现在,您似乎正在将地图视图的框架设置为与单元格的边界相同。相反,我会为做同样基本事情的地图视图定义约束:

let mapView = MKMapView()
mapView.userInteractionEnabled = false
mapView.setTranslatesAutoresizingMaskIntoConstraints(false)
contentView.addSubview(mapView)
let views = ["mapView" : mapView]
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[mapView]|", options: nil, metrics: nil, views: views))
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[mapView]|", options: nil, metrics: nil, views: views))

当我配置它时(加上设置tableView的rowHeight),它在iOS 7和iOS 8中都很好。

如果你有可变或动态的单元格高度,那么这个过程就有点不同了。iOS 8比iOS 7更优雅地处理这类事情。

最新更新