gpt4 book ai didi

iOS Swift 将 2 个或更多 UITableView 放入带有数据源和委托(delegate)的 UIScrollView 中时出现问题

转载 作者:行者123 更新时间:2023-11-29 01:55:40 25 4
gpt4 key购买 nike

我正在尝试创建一个由 2 个或更多 UITableView 组成的 UIScrollView,用户可以从左向右滑动以在 UITableView 之间切换

到目前为止,我所做的是将 2 个 UITableView 放入 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 类,它还实现了 UITableViewDataSource 和 UITableViewDelegate 以及需要启动 UITableView 的 2 个必需函数(cellForRowAtIndexPathnumberOfRowsInSection)

但是一旦我尝试通过取消注释

将第二个 tableview ( tableView2) 连接到实例化的 SomethingViewController
//        tableView2.delegate = foo
// tableView2.dataSource = foo

我的程序因 EXEC_BAD_ACCESS 而崩溃

我在这里做错了什么?如果还有其他建议,我愿意接受其他建议,谢谢!

最佳答案

首先,您可能应该将对第二个 tableView 的引用存储在某处,而不仅仅是将其放入 ScrollView 中。但这不是您崩溃的原因。

其次:你崩溃的原因。您不存储对“foo”的引用,这意味着它将在 viewDidLoad 完成后被释放。

你可能应该这样做:

@IBOutlet var foo: SomethingViewController! 位于顶部

然后使用self.foo = SomethingViewController()

关于iOS Swift 将 2 个或更多 UITableView 放入带有数据源和委托(delegate)的 UIScrollView 中时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30897615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com