0

I am working on an app where the user can create game lists and i have managed to create he scene where the user can see the details of the list (Title and description) and get an overview of the games added in a UITableView.

When the user has added games to the tableView i resize the table and the scrollView content size to easily display all the games with the ScrollView. The problem lies in that the didSelectRowAt function is only called by the first two cells!! And i can't understand why it does not work for the rest of the cells.

The code for resizing:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if gamesInList.count > 0  && gamesInList.count != previourCount {
        previourCount = gamesInList.count
        gamesTableView.isHidden = false
        gamesInListTitle.isHidden = false
        listEditButton.isHidden = false

        // MARK: Resize views for the table view
        let oldTableSize = gamesTableView.frame.size.height
        print(gamesTableView.estimatedRowHeight)
        // Resi
        gamesTableView.frame = CGRect(x: gamesTableView.frame.origin.x, y: gamesTableView.frame.origin.y, width: gamesTableView.frame.size.width, height: CGFloat(gamesInList.count) * gamesTableView.estimatedRowHeight) // TODO Ond variabel!
        scrollView.contentSize = CGSize(width: contentView.frame.size.width, height: scrollView.contentSize.height - oldTableSize + gamesTableView.frame.height)

        gamesTableView.reloadData()
    }
}

I thought it had something to do with the Content View inside the scrollView not resizing so i tried to resize it with code as well but it did not work anyway.

Update

So i have been debugging my problem for a while and i think i have found the real issue. The scrollView contentSize is resized correctly but the contentView is not resized and stays the same. Resizing it programmatically does nothing as it just keeps the original size

Oreex0
  • 324
  • 3
  • 15
  • Is there something more i need to do with the UITableView to get it to recognize that there are more visible cells? – Oreex0 Mar 06 '18 at 09:28
  • You should check size of TableView and ScrollView, maybe something is overlapping the other – Tikhonov Aleksandr Mar 06 '18 at 09:30
  • I just checked, and it looks fine, the scrollview is larger than the tableview. Ex: scrollView.contentSize = (width=375, height = 962) tableView.frame.size.height = 565 – Oreex0 Mar 06 '18 at 09:35
  • I think that i have found the issue to be AutoLayout.. played around with the contstraints a bit and managed to improve the content view size. Everything that i tap outside of the content view does not register. What is the best way to set up the constraints for the content view to let it expand freely with the scroll view – Oreex0 Mar 06 '18 at 10:30

1 Answers1

0

So after about 5 hours of searching and testing i finally managed to solve this weird problem which is autolayout.

first step was to fix the constraints of the scroll view and the content view for it to dynamically expand with the content

the second and hardest step to fix was to actually manage to force the contentView to expand with the new scrollView contentSize. But looking at this post i found the solution to my problem. Not sure if this is a bit of a hack but you give the UITableView a height constraint (High) which you can drag into your code and then change that constraint which will force autolayout to do your bidding.

Finished code:

if gamesInList.count > 0  && gamesInList.count != previousCount {
        previousCount = gamesInList.count
        gamesTableView.isHidden = false
        gamesInListTitle.isHidden = false
        listEditButton.isHidden = false

        // MARK: Resize views for the table view
        let oldTableSize = gamesTableView.frame.size.height

        self.gamesTableView.frame.size = CGSize(width: gamesTableView.frame.size.width, height: CGFloat(gamesInList.count) * gamesTableView.estimatedRowHeight)

        scrollView.frame.size = CGSize(width: scrollView.frame.size.width, height: scrollView.contentSize.height - oldTableSize + gamesTableView.frame.height)

        tableViewContraint.constant = scrollView.contentSize.height

        gamesTableView.reloadData()

    }

Hope this helps!

Oreex0
  • 324
  • 3
  • 15