滑动视图控制器

时间:2017-10-14 13:07:00

标签: swift uitableview swift3

我设法以编程方式制作幻灯片菜单控制器。我试图在tableViewController中添加一个转换,当你单击一个标签关闭菜单时,动画将使tableView全屏显示0.2秒,然后关闭菜单。这将有时间通过​​单击按钮更改视图,看起来不错。在didSelectRowAtIndexPath中有没有。它有效!

tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)

因为我只打开80%的屏幕菜单,我正在移动单元格的contentView以显示在80%的屏幕上。当动画在didSelectRowAtIndexPath中开始时,我将转回0,所以我不会在tableView的左侧看到一个空格,如下所示:

tableView.contentInset = UIEdgeInsetsMake(0, menuWidthGap, 0, -menuWidthGap)

现在,当我再次点击该按钮时,它仍然是这样的。当我再次单击该按钮以返回到contentView的初始状态时,我该怎么办。

这是我的tableView:

    import UIKit

class MenuLeftVC: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Initialize the window
        let window = UIWindow.init(frame: UIScreen.main.bounds)
        window.makeKeyAndVisible()

        view.backgroundColor = UIColor.yellow
        navigationController?.isNavigationBarHidden = true

        tableView.dataSource = self
        tableView.delegate = self
        tableView.showsVerticalScrollIndicator = false

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")



    }


    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)
        print("did apear")
        tableView.contentInset = UIEdgeInsetsMake(0, menuWidthGap, 0, -menuWidthGap)
    }

    func selectCell() {
        print("selected cell - >")

        NotificationCenter.default.post(name: NSNotification.Name("SelectedCell"), object: nil)
    }

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

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
        cell.contentView.reloadInputViews()

        cell.isSelected = false
        tableView.contentInset = UIEdgeInsetsMake(0, menuWidthGap, 0, -menuWidthGap)


        cell.contentView.backgroundColor = UIColor.yellow
        cell.textLabel?.text = "Daim Boy"


        return cell
    }


    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)!
        selectedCell.contentView.backgroundColor = UIColor.red

        selectedCell.isSelected = false

        if selectedCell.isSelected == true {
        print("selection")
        if selectedCell.textLabel?.text == "Daim Boy" {
            print("cell is selected")
            UIView.animate(withDuration: 0.3, animations: {
                tableView.contentInset = UIEdgeInsetsMake(0, menuWidthGap, 0, -menuWidthGap)
                self.selectCell()
                //selectedCell.isSelected = false

            })

        }

        }


    }






}

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题:(这很简单)我只是调用viewWillAppear,我在这里放置了代码,以便在函数结束时使用DispatchQue开始布局:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)!
    selectedCell.contentView.backgroundColor = UIColor.red

    selectedCell.isSelected = false

    if selectedCell.isSelected == true {
    print("selection")
    if selectedCell.textLabel?.text == "Daim Boy" {
        print("cell is selected")
        UIView.animate(withDuration: 0.3, animations: {
            tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
            self.selectCell()
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: { 
                self.viewWillAppear(true)
            })

            //selectedCell.isSelected = false

        })

    }

    }


}

viewWillAppear包含:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    print("will appear")
    tableView.contentInset = UIEdgeInsetsMake(0, menuWidthGap, 0, -menuWidthGap)

}