0

I'm calling a TableView inside of a custom ViewController. In my UITableView, my cells overlap. Like so

TableviewCells Overlapping

In the storyboard editor, I have the Row Height set to Custom at 63pts, but they look like they're actually using the the default height that the cell would be if you unchecked the Custom box.

Like in some of the other solutions tried implementing the heightForRowAt function but it doesn't do anything. I've tried using obscenely large numbers, but the result is always exactly as it was before.

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    if(self.view.traitCollection.horizontalSizeClass == .compact ||
        self.view.traitCollection.verticalSizeClass == .compact) {
        cellHeight = 55
        return 55
    }
    else{
        return 63
    }
}

The only thing in solutions I've seen so far that looks at all promising is this one. iOS UITableViewCell overlapping I've tried implementing the prepareForReuse Function in my custom TableViewCell class file, but I didn't know what I was actually supposed to add to the function, so I just called the super function and added a print statement

--- Full TableView Code ---

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

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "WishlistTableViewCell"

        guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? WishlistTableViewCell else {
            fatalError("The dequeued cell is not an instance of WishlistTableViewCell.")
        }

        cell.selectionStyle = .none


        return cell
    }


    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
        if(self.view.traitCollection.horizontalSizeClass == .compact ||
            self.view.traitCollection.verticalSizeClass == .compact) {
            cellHeight = 55
            return 55
        }
        else{
            return 63
        }
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            /*
            let resultRemoving = bookmarks[indexPath.row]
            if var bookmarkData = defaults.object(forKey:"bookmarkDict") as? [String: [String]] {
                bookmarkData.removeValue(forKey: resultRemoving.resultURL)
                defaults.set(bookmarkData, forKey:"bookmarkDict")
            }
            defaults.synchronize()
            */
            items.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
srey
  • 5
  • 1

1 Answers1

1

Currently heightForRowAt is not being called since heightForRowAt method is a UITableViewDelegate method so

Your extension should look like this:

Add UITableViewDelegate

extension SavingViewController: UITableViewDataSource, UITableViewDelegate {
}
3stud1ant3
  • 3,586
  • 2
  • 12
  • 15