简体   繁体   English

Swift TableViewCells 没有出现

[英]Swift TableViewCells not showing up

I got a ViewController , with two UITableView in it.我有一个ViewController ,里面有两个UITableView 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.我的第二个 Tableview 没有显示所有项目,只有 5 个。

The TableViews are both dynamic and size themself. TableViews 既是动态的,又是自己调整大小的。

Here is my cellForRowAt function:这是我的cellForRowAt函数:

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.正如评论的那样,第一个 tableview 是不能正常工作的。 It only runs 5 times, when I watch it with the debugger, but there are more Items in the Array.当我用调试器观察它时,它只运行了 5 次,但数组中有更多的项目。

Edit:编辑:

Here is my numberOfRowsInSection function这是我的numberOfRowsInSection函数

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

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

Edit 2:编辑2:

When I enable scrolling in my TableView and then try to scroll, all the items appear.当我在TableView启用滚动然后尝试滚动时,所有项目都会出现。 But the scrolling should be disabled, because it automatic resizes (which works)但是应该禁用滚动,因为它会自动调整大小(有效)

Final edit:最终编辑:

I added like some more height to the tableView and enabled scroll (but disabled the bounce, so u don't see anything from the scroll)我在tableView添加了更多高度并启用了滚动(但禁用了反弹,所以你看不到滚动中的任何内容)

Now it loads properly and everything works fine现在它可以正常加载并且一切正常

A UITableView works with recycling UITableViewCell s. UITableView与回收UITableViewCell一起使用。 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).这意味着 tableview 只会首先加载可见单元格,并且只有当您滚动时,它才会加载更多单元格(通过回收旧单元格)。

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).我可以看到的问题是您可能会尝试获取 tableview 的高度,以更新高度约束(因此 tableview 完整显示)。 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).但这可能是一个不正确的计算(如前所述,它只实例化前 x 个单元格,因此它无法知道剩余单元格的高度)。 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).此外,永远不应从cellForRowAt执行此计算,因为该方法不应在单元cellForRowAt执行任何操作(除非您想知道单元格何时变得可见)。

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.您可以尝试考虑将所有其他应在 tableview 上方和下方滚动的视图放入 tableviewcells 中。 Then you can just use a regular tableview, no fancy height constraints and calculations needed.然后你可以只使用一个普通的 tableview,不需要花哨的高度限制和计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM