简体   繁体   English

UITableView contentSize不会在ios7中更新

[英]UITableView contentSize doesn't update in ios7

I have a UITableViewController which loads some cells normally using the datasource delegate methods. 我有一个UITableViewController,通常使用datasource委托方法加载一些单元格。

In ios8 everything works fine, but in ios7 the contentsize height doesnt seem to get the right height for the cells. 在ios8中一切正常,但在ios7中,内容高度似乎没有得到正确的单元格高度。

It adds the cells correctly which goes off-screen in the bottom, and when I want to scroll down it doesnt scroll, it just starts the bounce animation like you were at the bottom. 它正确地添加了在屏幕下方的单元格,当我想向下滚动时它不会滚动,它只是像你在底部一样启动反弹动画。 When I drag I can see the cells below the bottom of the screen but when I release it bounce back. 当我拖动时,我可以看到屏幕底部下方的单元格,但是当我释放它时会反弹。

I think its just some property im missing but I cant seem to find which one. 我认为它只是一些属性我失踪但我似乎无法找到哪一个。

Im not configuring the table view at all, I just use the datasource to add the cells to it. 我根本没有配置表视图,我只是使用数据源来添加单元格。

EDIT 编辑

I am using autolayout. 我正在使用autolayout。

When I print out the contentsize it prints out CGSizeZero 当我打印出内容时,它打印出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 is set in a prepareSegue function in another view before this one. BookingInfo在此之前的另一个视图中的prepareSegue函数中设置。

ListCell is a custom cell that loads a map using coordinates inside BookingInfo. ListCell是一个自定义单元格,使用BookingInfo内的坐标加载地图。

Im changing the cell ID for quick access when sorting so the map doesn't have to reload. 我在更改单元ID时可以在排序时快速访问,因此地图不必重新加载。

EDIT 2 编辑2

Im using MapBox 我正在使用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
    }
}

There is more in this class but they dont have anything to do with the map, they are just simple UILabels, removed them in question so it woudnt get too long. 这个类中有更多,但它们与地图没有任何关系,它们只是简单的UILabel,将它们删除,因此它会变得太长。

EDIT AFTER @ROB's ANSWER 编辑@ ROB的答案之后

I added constraints but still contentSize becomes {0, 0} 我添加了约束但仍然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)

Right now, you appear to be setting the frame of the map view to be the same as the bounds of the cell. 现在,您似乎将地图视图的框架设置为与单元格的边界相同。 I would, instead, define constraints for the map view that does the same basic thing: 相反,我会为地图视图定义执行相同基本操作的约束:

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))

When I configured that (plus set the rowHeight of the tableView), it appeared fine in both iOS 7 and iOS 8. 当我配置它(加上设置tableView的rowHeight )时,它在iOS 7和iOS 8中都显得很好。

If you have variable or dynamic cell heights, then the process is a little different. 如果您具有可变或动态的单元格高度,那么该过程会有所不同。 iOS 8 handles this sort of stuff more gracefully than iOS 7 does. iOS 8比iOS 7更优雅地处理这类内容。

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

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