【发布时间】: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