简体   繁体   中英

TableView scrolls beyond the last cell

I have 2 TableViews in a ViewController and I setup the constraints for both of them. 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).

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'. 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

在此输入图像描述

(Red is TableView1 and Pink is 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.

(Blue is TableView1's background color)

在此输入图像描述

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?

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.

Why can this be happened?


Update2: I am pretty sure that it's not the code, but here is the code

TableView1 constraints:

在此输入图像描述

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. 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. 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!
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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