【发布时间】:2016-11-25 03:50:34
【问题描述】:
当 UITableView 中没有可显示的内容时,我使用 DZNEmptySet 进行加载。它适用于 DZNEmptySet 的方法,如 titleForEmptyDataSet、imageForEmptyDataSet,但不是我想要使用的方法(即 customViewForEmptyDataSet)。
当我尝试将 xib 加载到 scrollView.frame 时,Xcode 的内存开始以 30 兆字节的增量膨胀,并且应用程序挂起。我知道我有错,但我不知道我在搞砸什么。
我在这里查看了很多答案和其他网站上的教程,但我找不到适合这种情况的解决方案(我认为这很简单)。任何有关这方面的反馈将不胜感激。
这是customViewForEmptyDataSet MyViewController
// App hangs on this and memory bloats in 30 megabyte increments.
func customViewForEmptyDataSet(scrollView: UIScrollView!) -> UIView! {
return EmptySetView(frame: scrollView.frame)
}
这是我正在尝试初始化的 EmptySetView 的类:
import UIKit
class EmptySetView: UIView {
var view: UIView!
// These are connected to a xib
@IBOutlet weak var backgroundImageView: UIImageView!
@IBOutlet weak var viewLabel: UILabel!
@IBOutlet weak var viewTextView: UITextView!
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup() {
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
self.addSubview(self.view)
}
func loadViewFromNib() -> UIView {
let bundle = NSBundle(forClass:self.dynamicType)
let nib = UINib(nibName: "EmptySetView", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
return view
}
}
更新
在Matt's suggestion,我调查了我遇到的递归并发现了问题的根源。
您没有在 xib 的 View 上指定 UIView 类。相反,您单击标题为 File's Owner 的黄色立方体并在其中指定 UIView,在 Document Outline 面板的 View 上将类字段留空。
清理缓存和重建后,我可以使用在MyViewController 上调用的这段代码将视图加载到层次结构中:
func customViewForEmptyDataSet(scrollView: UIScrollView!) -> UIView! {
let emptySetView = EmptySetView(frame: scrollView.frame)
return emptySetView
}
在加载 xib 之前,EmptySetView xib 和 MyViewController 彼此不知道,因此您需要处理布局约束。
【问题讨论】:
-
你从哪里调用'customViewForEmptyDataSet'
-
使用仪器。它会立即告诉您问题的根源是什么。您可以观察内存增长并查看导致它的调用链。
-
@NickCatib
customViewForEmptyDataSet是一个委托方法,如果tableView为空,则会调用该方法。 -
@matt 我把它放在 Instruments for Leaks 中,我认为我的问题在于 ` required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) setup() }
. I think thesetup()` 调用导致问题。我看过的每个示例都有 init frame 和 init coder w/ 看起来几乎相同。我会继续努力,放心,我正在做一些非常愚蠢的事情。 -
我确定你在无限递归,但我太累了,无法告诉你在哪里。调试器会立即显示给您。
标签: ios swift autolayout scrollview xib