我正在尝试创建一个从 Cocoapod 类返回 UITableView 的函数。但是,当我调用该函数时,我得到:
Thread 1: EXC_BAD_ACCESS (code=2, address=0x7fff89c111b0)
当我在主 ViewController 中初始化 TableView 时,不会发生这种情况。
例如
let tableView = UITableView()
这可以正常工作。
let tableView = laneScroller.makeTable()
但是这失败了。
这是我的 Cocoapod 类的作用:
public class LaneScroller: NSObject {
private var size: CGFloat
public init(size: CGFloat) {
self.size = size
}
public func makeView() -> UIView {
return UIView(frame: CGRect(x: 0, y: 0, width: size, height: size))
}
public func makeTable() -> UITableView {
let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
tableView.delegate = self
tableView.dataSource = self
return tableView
}
}
我已经在扩展中实现了委托(delegate)和数据源方法。
如您所见,我使用了类似的方法来获取 UIView 并将其添加到我的 UIViewController 的 View 中,效果非常好。我在某处读到这可能与弱分配和强分配有关。但不知道如何解决这个问题。
最佳答案
您需要从函数体中删除这两行
tableView.delegate = self
tableView.dataSource = self
并将其添加到函数返回 tableView 实例的位置,因为您在 UiViewController 扩展中实现了 tableView 数据源和委托(delegate),并且 Lanescroller 不是从 UIViewController 继承的,或者我可以说它不是 View Controller 。
关于ios - 返回 UITableView 的函数会导致 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58350054/