简体   繁体   中英

How to keep Tableview scrolling and how to delete empty space between prototypecell and tableview in Swift3

I have two questions regarding tableview in Swift3

Here my tableview is not scrolling and even i uncheck the adjust scroll view insets in UiView attribute inspector then also empty space above the cell is not deleting.

Here why my tableview is not scrolling even the scroll is enabled How to scroll tableview is there any thing to do in my code

Please help me in the two questions

I have written code like below

import UIKit

class movieViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    @IBOutlet weak var tableviewLayout: UITableView!

    var itemsArray = ["One","Two","Three","Four","Five","six","Seven","Eight","Nine","Ten"]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableviewLayout.isScrollEnabled = true
        scrollToLastRow()
    }

    override func viewWillAppear(_ animated: Bool) {
        self.tableviewLayout.contentInset = UIEdgeInsetsMake(-35, 0, 0, 0);
      //only this is working   
      //but when i uncheck the adjust scroll view insets why it is not working
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return itemsArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {  
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel!.text = itemsArray[indexPath.row]
        return cell
    }

    func scrollToLastRow() {
        let indexPathrow = NSIndexPath(row: itemsArray.count - 1, section: 0)
        self.tableviewLayout.scrollToRow(at: indexPathrow as IndexPath , at: UITableViewScrollPosition.bottom, animated: true)
    }

}

scrollToLastRow() will not work in the viewDidLoad method. You should use it in the viewDidAppear method and it will work fine.

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