yysskk / SwipeMenuViewController

Swipable tab and menu View and ViewController.
MIT License
1.29k stars 131 forks source link

swipe menu view controller cells not showing #77

Closed 4ndrewHarri5 closed 5 years ago

4ndrewHarri5 commented 5 years ago

I am having a problem with implementing the swipeMenuView into my project. When first loading the NotificationsViewController it shows the cell I want fine, however, when I start scrolling/tapping or doing anything the whole table resets and removes the cell along with changing the cell height to default. I have a feeling it is the way I am implementing the swipeMenuView.

Here is what it looks like when first loading:

img_7563

and after touching or scrolling the view:

img_7564

The way I implement:

`import SwipeMenuViewController

class NotificationsViewController: UIViewController {

@IBOutlet weak var swipeMenuView: SwipeMenuView! {
    didSet {
        swipeMenuView.delegate = self
        swipeMenuView.dataSource = self
        var options: SwipeMenuViewOptions = .init()
        options.tabView.style = .segmented
        options.tabView.additionView.backgroundColor = UIColor.black 
        options.tabView.itemView.textColor = UIColor.darkGray
        options.tabView.itemView.selectedTextColor = UIColor.black
        swipeMenuView.reloadData(options: options)
    }
}
var tabTitles: [String] = ["All", "Friend Requests", "Items"]
var requests = [User]()

override func viewDidLoad() {
    super.viewDidLoad()
    self.observeRequests()
}

func observeRequests() { //This just gets the friends requests of that user, there are no problems here

    currentUser.addRequestObserver { (user) in

        if self.requests.filter({ $0.uid == user.uid }).first != nil {
            print("found the request in the array")
        } else {
            self.requests.insert(user, at: 0)
            self.swipeMenuView.reloadData()
        }
    }
}
}`

The delegate and data source

`extension NotificationsViewController: SwipeMenuViewDelegate {

// MARK - SwipeMenuViewDelegate
func swipeMenuView(_ swipeMenuView: SwipeMenuView, viewWillSetupAt currentIndex: Int) {
    // Codes
}

func swipeMenuView(_ swipeMenuView: SwipeMenuView, viewDidSetupAt currentIndex: Int) {
    // Codes
}

func swipeMenuView(_ swipeMenuView: SwipeMenuView, willChangeIndexFrom fromIndex: Int, to toIndex: Int) {
    // Codes
}

func swipeMenuView(_ swipeMenuView: SwipeMenuView, didChangeIndexFrom fromIndex: Int, to toIndex: Int) {
    // Codes
}
}

extension NotificationsViewController: SwipeMenuViewDataSource {

//MARK - SwipeMenuViewDataSource
func numberOfPages(in swipeMenuView: SwipeMenuView) -> Int {
    return tabTitles.count
}

func swipeMenuView(_ swipeMenuView: SwipeMenuView, titleForPageAt index: Int) -> String {
    return tabTitles[index]
}

func swipeMenuView(_ swipeMenuView: SwipeMenuView, viewControllerForPageAt index: Int) -> UIViewController {
    let vc = NotificationsContentTable()
    vc.currentUser = currentUser
    vc.requests = requests
    print("the requests are in = \(requests)")
    return vc
}
}`

Inside of NotificationsContentTable()

class NotificationsContentTable: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var currentUser: User!
    var requests = [User]()

    private var myTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let barHeight: CGFloat = 103
        let displayWidth: CGFloat = self.view.frame.width
        let displayHeight: CGFloat = self.view.frame.height

        myTableView = UITableView(frame: CGRect(x: 0, y: 0, width: displayWidth, height: displayHeight))
        myTableView.register(UINib(nibName: "FriendRequestTableViewCell", bundle: nil), forCellReuseIdentifier: "friendRequest")
        myTableView.dataSource = self
        myTableView.delegate = self
        self.view.addSubview(myTableView)

    }

    // MARK: - Table view data source

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

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = Bundle.main.loadNibNamed("FriendRequestTableViewCell", owner: self, options: nil)?.first as! FriendRequestTableViewCell

        cell.currentUser = currentUser
        cell.requestFriendDelegate = self
        cell.user = requests[indexPath.row]

        return cell

    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 103
    }

}
yysskk commented 5 years ago

Hi, @4ndrewHarri5. See also https://github.com/yysskk/SwipeMenuViewController/issues/26

4ndrewHarri5 commented 5 years ago

Thank you. Totally missed that issue! It now works.