简体   繁体   中英

UITableView contentSize doesn't update in ios7

I have a UITableViewController which loads some cells normally using the datasource delegate methods.

In ios8 everything works fine, but in ios7 the contentsize height doesnt seem to get the right height for the cells.

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.

When I print out the contentsize it prints out 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.

ListCell is a custom cell that loads a map using coordinates inside BookingInfo.

Im changing the cell ID for quick access when sorting so the map doesn't have to reload.

EDIT 2

Im using 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.

EDIT AFTER @ROB's ANSWER

I added constraints but still contentSize becomes {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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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