最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[未解决]swift中tableview添加到scrollView后无法正常显示

iOS crifan 2889浏览 0评论

折腾:

[已解决]swift中让一个列表的高度随着内容的可视区域增加而增加

期间,改为:

<code>class HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    var contentView:UIScrollView

    override func viewDidLoad() {
        super.viewDidLoad()

        if let tabBarController = self.tabBarController {
            TabBarHeight = tabBarController.tabBar.frame.height //49
        }

        self.view.backgroundColor = AppBackgoundColor

        self.view.addSubview(self.contentView)
        constrain(self.contentView) {contentView in
            contentView.top == contentView.superview!.top
            contentView.left == contentView.superview!.left
            contentView.right == contentView.superview!.right
            contentView.bottom == contentView.superview!.bottom - TabBarHeight
        }

        //3. Today Todo TableView
        initTaskItem()

        self.todoTableView.delegate = self
        self.todoTableView.dataSource = self
        self.todoTableView.tableFooterView = UIView()
        //self.todoTableView.backgroundColor = UIColor.whiteColor()
        self.todoTableView.backgroundColor = UIColor.clearColor()
//        self.todoTableView.estimatedRowHeight = UITableViewAutomaticDimension
        self.todoTableView.registerClass(TaskTableViewCell.self, forCellReuseIdentifier: TaskTableViewCellId)
        //self.view.addSubview(self.todoTableView)
        self.contentView.addSubview(self.todoTableView)
        constrain(todoTableView, newView) {todoTableView, newView in
            todoTableView.top == newView.bottom + 10
            todoTableView.left == todoTableView.superview!.left
            todoTableView.right == todoTableView.superview!.right
            //todoTableView.height == HomeNewHeight
//            todoTableView.bottom == todoTableView.superview!.bottom - TabBarHeight
            todoTableView.bottom == todoTableView.superview!.bottom
        }
}
</code>

结果没添加scrollView的contentView之前,还可以正常显示的位于最下面的tableview,显示无法显示了。

去调试,发现:

<code>    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        gLog.verbose("self.view.frame\(self.view.frame), self.contentView.frame=\(self.contentView.frame), self.todoTableView.frame=\(self.todoTableView.frame)")
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

        gLog.verbose("self.view.frame\(self.view.frame), self.contentView.frame=\(self.contentView.frame), self.todoTableView.frame=\(self.todoTableView.frame)")
}
</code>

输出:

<code>2016-05-25 11:19:54.422 [Verbose] [main] [HomeViewController.swift:317] viewWillLayoutSubviews() &gt; self.view.frame(0.0, 0.0, 320.0, 504.0), self.contentView.frame=(0.0, 0.0, 0.0, 0.0), self.todoTableView.frame=(0.0, 0.0, 0.0, 0.0)
2016-05-25 11:19:54.431 [Verbose] [main] [HomeViewController.swift:310] viewDidLayoutSubviews() &gt; self.view.frame(0.0, 0.0, 320.0, 504.0), self.contentView.frame=(0.0, 0.0, 320.0, 455.0), self.todoTableView.frame=(0.0, 0.0, 0.0, 0.0)
</code>

即:tableView的尺寸都是0了。

1.去设置scrollview的contentsize:

<code>       self.contentView.contentSize = CGSizeMake(self.view.frame.width, self.view.frame.height - TabBarHeight)
</code>

问题依旧。

2.swift tableview inside scrollview not show

ios – UITableView inside UIScrollView not receiving first tap after scrollling – Stack Overflow

UIWebView Class Reference

“IMPORTANT

You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.”

Technical Note TN2154: UIScrollView And Autolayout

->所以,此处把tableview放在scrollview里面,本身就是不合适的

-》就会导致各种不正常的现象,所以要避免这种现象的出现。。

不过,去试试:让scrollview可以滚动,但是内部的tablebiew不可以滚动:

<code>        self.contentView.scrollEnabled = true

       self.todoTableView.scrollEnabled = false
</code>

问题依旧。

-》把之前,tableview上面的部分,都放到headerview中,那样就不用麻烦的,需要外部的scrollview了。

-》也不用我后来想到的(相对有点麻烦的):

把外部的顶部的那些view,当作tableview的不同的section的了

加上了:

<code>self.contentView.translatesAutoresizingMaskIntoConstraints = false
self.statisticView.translatesAutoresizingMaskIntoConstraints = false
self.newView.translatesAutoresizingMaskIntoConstraints = false
self.todoTableView.translatesAutoresizingMaskIntoConstraints = false
</code>

问题依旧。

-》所以,去掉这种用法

确保不要把tableview放在scrollview中,详见:

[已解决]swift中让一个列表的高度随着内容的可视区域增加而增加

-》按照道理来说,

应该是可以,按照上面的帖子,去实现:

把tableview放在scrollview中,并且仍然可以正常工作的。

只是此处没时间继续研究了。

暂时放弃,有空的话,再折腾。

转载请注明:在路上 » [未解决]swift中tableview添加到scrollView后无法正常显示

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.163 seconds, using 22.10MB memory