![](/img/trans.png)
[英]Swift UITableView delegate and dataSource declaration and retain cycles
[英]iOS Swift Problems putting 2 or more UITableView in a UIScrollView with datasource and delegate
我正在尝试创建一个包含2个或更多UITableViews的UIScrollView,并且用户可以从左向右滑动以在UITableViews之间切换
到目前为止,我所做的是将2个UITableViews放入UIScrollView中,如下所示:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var theScroll: UIScrollView!
@IBOutlet var tableView1: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView1 = UITableView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
self.tableView1.delegate = self
self.tableView1.dataSource = self
var foo = SomethingViewController()
var tableView2:UITableView = UITableView(frame: CGRectMake(self.view.frame.width, 0, self.view.frame.width, self.view.frame.height))
// tableView2.delegate = foo
// tableView2.dataSource = foo
self.theScroll.addSubview(tableView1)
self.theScroll.addSubview(tableView2)
self.theScroll.contentSize = CGSizeMake(self.view.frame.width * 2, 0)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel?.text = "hello"
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
}
我还实现了您可能会注意到的SomeViewController
类,该类还使用需要启动UITableView的2个必需函数( cellForRowAtIndexPath
和numberOfRowsInSection
)实现UITableViewDataSource和UITableViewDelegate。
但是一旦我尝试通过取消注释第二个tableview( tableView2
)到实例化的SomethingViewController
// tableView2.delegate = foo
// tableView2.dataSource = foo
我的程序因EXEC_BAD_ACCESS
崩溃
我在这里做错了什么? 如果还有另一个我愿意接受其他建议,谢谢!
首先,您可能应该将对第二个tableView的引用存储在某个地方,而不仅仅是将其放入滚动视图中。 但这不是崩溃的原因。
其次:崩溃的原因。 您不存储对“ foo”的引用,这意味着它将在viewDidLoad完成后被释放。
您可能应该执行以下操作:
@IBOutlet var foo: SomethingViewController!
在顶部
然后使用self.foo = SomethingViewController()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.