【问题标题】:UITableView contentSize doesn't update in ios7UITableView contentSize 不会在 ios7 中更新
【发布时间】:2015-05-04 04:39:41
【问题描述】:

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

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

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

我认为它只是缺少一些属性,但我似乎找不到哪个属性。

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

编辑

我正在使用自动布局。

当我打印出 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!
}

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

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)

【问题讨论】:

  • @Rob 我更新了问题
  • @Rob 它是一个 UITableViewController,所以 tableview 的框架会覆盖整个屏幕。
  • 问题是,我认为它与我的代码无关,我认为它是一个 ios7 的行为,把事情搞砸了。
  • 我删除了地图,然后它工作正常......它真的很奇怪,它只在 ios7 中搞砸了。我将更新如何将地图添加到 ListCell。
  • @Rob 如果我删除了将地图添加到 contentView 的行,它会像它假设的那样工作......我将它添加到 disptach_after 中,5 秒后它添加了地图,并在添加后立即添加tableView contentSize 变为 CGSizeZero ...我现在很困惑,不知道该怎么办。

标签: ios uitableview ios7 uiview scrollview


【解决方案1】:

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

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 更优雅地处理这类事情。

【讨论】:

  • 我将地图大小设置为 cellHeight、cellHeight,所以我将添加更多约束,看看是否有效。
猜你喜欢
  • 2013-03-04
  • 1970-01-01
  • 2015-08-08
  • 2013-12-29
  • 2014-10-19
  • 2013-09-15
  • 1970-01-01
  • 2014-04-23
  • 1970-01-01
相关资源
最近更新 更多