-2

I got a ViewController, with two UITableView in it. They both get data from the same object, but from different arrays inside that object.

My second Tableview doesn't show me all the items, only 5.

The TableViews are both dynamic and size themself.

Here is my cellForRowAt function:

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

        if tableView == tableViewSteps,  //This is the table that doesn't show right
            let cell = tableView.dequeueReusableCell(withIdentifier: "stepsCell", for: indexPath) as? StepsTableViewCell {

            let text = detailItem?.itemStepAr[indexPath.row] as! String

            cell.textAreaOutlet.text = "Schritt \(indexPath.row + 1) \n\(text)"

            let textAsNSString = cell.textAreaOutlet.text as NSString
            let lineBreakRange = textAsNSString.range(of: "\n")
            let newAttributedText = NSMutableAttributedString(attributedString: cell.textAreaOutlet.attributedText)
            let boldRange: NSRange
            if lineBreakRange.location < textAsNSString.length {
                boldRange = NSRange(location: 0, length: lineBreakRange.location)
            } else {
                boldRange = NSRange(location: 0, length: textAsNSString.length)
            }

            newAttributedText.addAttribute(NSAttributedString.Key.font, value: UIFont.preferredFont(forTextStyle: UIFont.TextStyle.headline), range: boldRange)

            cell.textAreaOutlet.attributedText = newAttributedText

            let fixedWidth = cell.textAreaOutlet.frame.size.width
            let newSize = cell.textAreaOutlet.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
            cell.textAreaOutlet.frame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
            cell.textAreaOutlet.isScrollEnabled = false
            tableStepsHeight = tableStepsHeight + Int(newSize.height)

            cell.textAreaOutlet.textColor = UIColor.lightGray

            tableViewSteps.frame = CGRect(x:0, y: currentViewHeight + 20, width: 375, height: tableStepsHeight)
            changeScrollHeight()
            return cell

        }

        else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ingredsCell")! //1.
        let text = detailItem?.itemIngredAr[indexPath.row]

        cell.textLabel?.text = text as? String

        cell.textLabel?.font = UIFont(name: "Futura-Medium", size: 17)

        cell.textLabel?.textColor = UIColor.lightGray

        return cell
        }
    }

as commented, the first tableview is the one, that doesn't work properly. It only runs 5 times, when I watch it with the debugger, but there are more Items in the Array.

Edit:

Here is my numberOfRowsInSection function

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

        if (tableView == tableViewSteps) {
            return detailItem?.itemStepAr.count ?? 0
        }
        else {
        return detailItem?.itemIngredAr.count ?? 0
        }
    }

Edit 2:

When I enable scrolling in my TableView and then try to scroll, all the items appear. But the scrolling should be disabled, because it automatic resizes (which works)

Final edit:

I added like some more height to the tableViewand enabled scroll (but disabled the bounce, so u don't see anything from the scroll)

Now it loads properly and everything works fine

  • 1
    show your `numberOfRowsInSection` method – Dharmesh Kheni Mar 28 '19 at 11:51
  • What do you have in `func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int`? – tanz Mar 28 '19 at 11:55
  • @Dharmesh I edited my post with the function – Lennard Klein Mar 28 '19 at 11:57
  • I suggest printing the values being returned in `numberOfRowsInSection` to verify that what you think is in the array matches what is reported. – Phillip Mills Mar 28 '19 at 12:02
  • @LennardKlein is this `changeScrollHeight()` calculate any size of your `TableView`? – Amir Khan Mar 28 '19 at 12:02
  • your `numberOfRowsInSections` is being called when you debug it? – Reinier Melian Mar 28 '19 at 12:04
  • @AmirKhan no, this calculates the size of the whole view, `tableViewSteps.frame = CGRect(x:0, y: currentViewHeight + 20, width: 375, height: tableStepsHeight)` calculates the `TableView` height – Lennard Klein Mar 28 '19 at 12:07
  • @PhillipMills it prints out the correct value – Lennard Klein Mar 28 '19 at 12:08
  • @ReinierMelian yes, it works as it should. I updated my question again – Lennard Klein Mar 28 '19 at 12:11
  • @LennardKlein AutomaticSize doesn't meant you need to disable tableview scrolling. – dahiya_boy Mar 28 '19 at 12:12
  • @dahiya_boy Why, I don't need to scroll, when the `tableView` has the correct height to fit all the cells. Also, when I enable scroll, I need to scroll once before the other cells appear – Lennard Klein Mar 28 '19 at 12:21
  • @LennardKlein - it sounds like you are not calculating your table view's frame to fit all the rows. – DonMag Mar 28 '19 at 12:24
  • Sounds as if there's some mismatch between the number of cells you see and the number that the table is treating as being displayed. – Phillip Mills Mar 28 '19 at 12:24
  • @DonMag Yes, when I enable scroll there is like 5 pixel I can scroll around, I added them to the `tableView` and it works fine – Lennard Klein Mar 28 '19 at 12:25
  • @DonMag He is calculating tableview height in cellForRow.. that's the issue. – dahiya_boy Mar 28 '19 at 12:43
  • @LennardKlein - you'll be much better off using constraints and auto-layout, rather than manually counting heights. Here's an example that uses an "auto-sizing" table view, where the top table grows and "pushes down" the bottom table. You should be able to easily adapt it to work with your scroll view layout: https://stackoverflow.com/a/55068380/6257435 – DonMag Mar 28 '19 at 12:48

1 Answers1

0

A UITableView works with recycling UITableViewCells. This means that a tableview will only load the visible cells first, and only when you scroll, will it load more cells (by recycling the old cells).

The issue I can see is that you may try to get the height of the tableview, to update a height constraint (so the tableview shows in its entirety). But that may be an incorrect calculation (as said before, it only instantiates the first x cells, so it can't know the height of the remaining cells). Also, this calculation should never be performed from cellForRowAt, as that method should never do things outside the cell (unless you want to know when a cell has become visible).

You can fix this by making your cells all have a predefined height, and then multiplying that by the number of cells.

If you have self-sizing cells, it can't be done (read: it can be done, but it's too unwieldy and inefficient to be practical).

You could try to look into taking all other views that should scroll above and below the tableview, into tableviewcells. Then you can just use a regular tableview, no fancy height constraints and calculations needed.

vrwim
  • 13,020
  • 13
  • 63
  • 118
  • I did this method with the first tableview, there I got fixed height. But on the second tableview I don't know the height of each cell, because it depends on how much text user put in. – Lennard Klein Mar 28 '19 at 14:23
  • See my last paragraph, don’t try to do things with `UITableView` that it wasn’t made for. – vrwim Mar 28 '19 at 14:24