[英]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.