简体   繁体   English

TableView滚动到最后一个单元格之外

[英]TableView scrolls beyond the last cell

I have 2 TableViews in a ViewController and I setup the constraints for both of them. 我在ViewController中有2个TableView,我为它们设置了约束。 I have also setup the constraints and alignments. 我还设置了约束和对齐。 The problem is that, when it starts, the alignments work as expected, however when I push TableView1 above (to come to the bottom), it pushes the last cell more (please check screenshots below). 问题在于,当它开始时,对齐按预期工作,但是当我按下上面的TableView1(到达底部)时,它会推动最后一个单元格(请查看下面的屏幕截图)。

Here is a video showing the problem 这是一个显示问题的视频

And the behaviour of horizontal inspector 和水平检查员的行为

Last Update: I thought of a workaround but I couldn't figure out how to apply it. 最后更新:我想到了一个解决方法,但我无法弄清楚如何应用它。 If I can get the y position of the last row inside TableView, maybe I can force the TableView not to scroll below 'y position + row height'. 如果我可以获取TableView中最后一行的y位置,也许我可以强制TableView不要在'y position + row height'下面滚动。 Is there a way I can do that? 有没有办法可以做到这一点?


Original question : 原始问题

It's working fine when it starts and seems as expected with last row/cell's bottom is attached to the bottom of the TableView 它在启动时工作正常并且看起来像预期的那样,最后一行/单元格的底部附加到TableView的底部

在此输入图像描述

(Red is TableView1 and Pink is TableView2) (Red是TableView1,Pink是TableView2)

But when scrolled up and reached the bottom, scrolls more than the bottom cell. 但是当向上滚动并到达底部时,滚动比底部单元格更多。 Instead bottom cell should be attached to the bottom of TableView1. 相反,底部单元格应该连接到TableView1的底部。

(Blue is TableView1's background color) (蓝色是TableView1的背景色)

在此输入图像描述

What may be the problem? 可能是什么问题? How can I make the bottom TableViewCell of TableView1 to attach to the bottom of TableView1, so doesn't get scrolled more? 如何将TableView1的底部TableViewCell附加到TableView1的底部,因此不会滚动更多?

Update: I made the TableView1's height 132 and each row 44. It's creating a gap as the same size of 44. But I checked numberOfRowsInSection , and the count is as expected, not +1. 更新:我使TableView1的高度为132,每行为44.它创建了一个与44相同大小的间隙。但是我检查了numberOfRowsInSection ,并且计数是预期的,而不是+1。

Why can this be happened? 为什么会这样?


Update2: I am pretty sure that it's not the code, but here is the code Update2:我很确定它不是代码,但这里是代码

TableView1 constraints: TableView1约束:

在此输入图像描述

let sampleData = [
    PreviewDetail(title: "Small"),
    PreviewDetail(title: "Medium"),
    PreviewDetail(title: "Large"),
    PreviewDetail(title: "Large")

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell:UITableViewCell?

    if tableView == self.TableView1 {
        cell = tableView.dequeueReusableCellWithIdentifier("TV1Cell", forIndexPath: indexPath)
        let previewDetail = sampleData[indexPath.row]
        cell!.textLabel!.text = previewDetail.title
    }
    return cell!
}

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var count:Int?

    if tableView == self.TableView1 {
        count = sampleData.count
    }
    return count!
}

Finally I've found the answer. 最后我找到了答案。 Weirdly, when I have the 'Deck of Views' as.. 奇怪的是,当我把'观景台'当作......

TabBarController -> NavBarController -> ViewController -> View -> 2xTableViews

..it was causing the buggy view. ..这导致了错误的观点。 I tried recreating the issue in a dummy project to understand the nature of the problem, and it was causing the same issue on the dummy project too. 我尝试在虚拟项目中重新创建问题以了解问题的本质,并且它也在虚拟项目中引起了同样的问题。

The Answer: 答案:

The answer is un-ticking Adjust Scroll View Insets in the ViewController. 答案是在ViewController中取消Adjust Scroll View Insets Now everything works fine. 现在一切正常。

Although there doesn't seem any mistakes in your code, when i tried recreating the scenario i couldn't reproduce it. 虽然您的代码中似乎没有任何错误,但当我尝试重新创建场景时,我无法重现它。 I used two UITableViews of equal width and both cell of tableViews are Default Basic. 我使用两个宽度相等的UITableViews,tableViews的两个单元格都是Default Basic。 Here is the code. 这是代码。

class ViewController: UIViewController {


@IBOutlet var tableView1:UITableView!
@IBOutlet var tableView2:UITableView!

let data1 = ["First Table, Data 1","First Table, Data 2","First Table, Data 3","First Table, Data 4","First Table, Data 5","First Table, Data 6","First Table, Data 7","First Table, Data 8"]
let data2 = ["Second Table, Data 1","Second Table, Data 2","Second Table, Data 3","Second Table, Data 4","Second Table, Data 5","Second Table, Data 6","Second Table, Data 7","Second Table, Data 8"]

override func viewDidLoad() {
    super.viewDidLoad()

    tableView1.dataSource = self
    tableView2.dataSource = self

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



}

extension ViewController:UITableViewDataSource
{
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        if tableView.isEqual(self.tableView1)
        {
            return data1.count
        }
        else if tableView.isEqual(self.tableView2)
        {
            return data2.count
        }
        return 0
    }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    if tableView == self.tableView1
    //if tableView.isEqual(self.tableView1)
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell1")
        cell?.textLabel?.text = data1[indexPath.row]
        return cell!
    }
    else
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell2")
        cell?.textLabel?.text = data2[indexPath.row]
        return cell!
    }
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM