Andrew Harris
Andrew Harris

Reputation: 416

Cells on UITableView disappearing after scrolling or touching the table

I am implementing a SwipeMenuViewController and when the user starts scrolling or tapping the cell disappears. I am not sure if it is due to the tableView being implemented not properly or the SwipeMenuViewController not being implemented properly.

Before swiping/touching the table, as you can see the table was loaded with the data

enter image description here

However. Once we start swiping or even touching the table, the previous cell disappears:

enter image description here

I have implemented the table view into a ViewController as follows

var currentUser: User!
var requests = [User]() 
var myTableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    myTableView = UITableView(frame: CGRect(x: 0, y: 0, width: tableSize.width, height: tableSize.height))
    myTableView.register(UINib(nibName: "FriendRequestTableViewCell", bundle: nil), forCellReuseIdentifier: "friendRequest")
    myTableView.dataSource = self
    myTableView.delegate = self
    //myTableView.separatorStyle = .none
    myTableView.rowHeight = 103
    myTableView.backgroundColor = Colours.flatColour.main.offWhite
    self.view.addSubview(myTableView)

}

The protocol methods:

// MARK: - Table view data source

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if (self.requests.count == 0) {
        self.myTableView.setEmptyMessage("You are up to date!\nYou dont have any notifications 😄")
    } else {
        self.myTableView.restore()
    }

    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
}

Does anyone have an idea as to where I have gone wrong, I am not sure if I am missing any code. The example of SwipeMenuViewController does not show this way of implementation. I have followed the Read me on implementing SwipeMenuView. I have two ViewControllers, one which holds the SwipeMenuView and one which holds the table for each tab which is the controller I think the issue is coming from. Thank you for any help

The notificationsViewController class:

class NotificationsViewController: UIViewController, SwipeMenuViewDelegate {

@IBOutlet weak var swipeMenuView: SwipeMenuView! {
    didSet {
        swipeMenuView.delegate = self
        swipeMenuView.dataSource = self
        var options: SwipeMenuViewOptions = .init()
        options.tabView.style = .flexible
        options.tabView.margin = 20.0
        options.tabView.additionView.backgroundColor = Colours.flatColour.yellow.lighter //UIColor.black//UIColor.customUnderlineColor
        options.tabView.itemView.textColor = Colours.flatColour.main.gray
        options.tabView.itemView.selectedTextColor = Colours.flatColour.yellow.lighter //UIColor.black//UIColor.customSelectedTextColor
        options.tabView.itemView.font = UIFont(name: "Biotif-Medium", size: 14)!

        swipeMenuView.reloadData(options: options)
    }
}

var tabTitles: [String] = ["All", "Friend Requests", "Items"]
var requests = [User]()

}

I reloadData again when populating the requests array and the array isnt empty. The data source methods:

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.tableSize = swipeMenuView.bounds.size
    if index == 0 {
        vc.requests = requests
    }else if index == 1 {
        vc.requests = []
    }else if index == 2 {
        vc.requests = []
    }
    print("the requests are in = \(vc.requests)")
    return vc
}

}

Upvotes: 2

Views: 519

Answers (1)

Andrew Harris
Andrew Harris

Reputation: 416

I've figured out where I was going wrong. Before returning the viewController in viewControllerForPageAt, you must add that viewController as a child with addChild(viewController).

Upvotes: 1

Related Questions