繁体   English   中英

UITableView contentSize不会在ios7中更新

[英]UITableView contentSize doesn't update in ios7

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

在ios8中一切正常,但在ios7中,内容高度似乎没有得到正确的单元格高度。

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

我认为它只是一些属性我失踪但我似乎无法找到哪一个。

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

编辑

我正在使用autolayout。

当我打印出内容时,它打印出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
    }
}

这个类中有更多,但它们与地图没有任何关系,它们只是简单的UILabel,将它们删除,因此它会变得太长。

编辑@ ROB的答案之后

我添加了约束但仍然contentSize变为{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更优雅地处理这类内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM