TableView TableTable内部的自动尺寸表视图 [英] TableView Automatic Dimension Tableview Inside Tableview

查看:82
本文介绍了TableView TableTable内部的自动尺寸表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Tableview用于显示客户下达的食物订单.其中包含水平UIStackview.在UIStackView

I have Tableview for showing food orders placed by clients. which contains Horizontal UIStackview. In UIStackView

UIStackview中,只有很少的一个/两行标签和一个UITableView用于显示orderItems.内部表格视图具有固定的高度常数(大于或等于+ 750优先级),并且将 随内容大小而改变.高度和滚动被禁用.

In UIStackview There is few one / two lined label and one UITableView which is used to show orderItems. Inner tableview has fixed height constant (greater then or equal + 750 Priority) and will be changed with content size. height and scrolling disabled.

内部表视图具有两个标签.而高度是自动尺寸

inner tableview has two labels. and heigh is automatic Dimension

我希望每个内部tableview项都应自动增加主tableview单元格的高度.所以我申请了主表视图

I want main tableview cell height should be increased automatically as per inner tableview items. so I apply to main tableview

    self.tblOrders.rowHeight = UITableViewAutomaticDimension
    self.tblOrders.estimatedRowHeight = 45

和在tableview单元格子类中

and In tableview cell subclass

override func awakeFromNib() {
    super.awakeFromNib()
    self.tblOrderMaster.dataSource = self
    self.tblOrderMaster.delegate = self

    self.tblOrderMaster.estimatedRowHeight = 45
    self.tblOrderMaster.rowHeight = UITableViewAutomaticDimension
    self.selectionStyle = .none
    self.layoutIfNeeded()

    self.tblOrderMaster.isScrollEnabled = false

}

内部tableview数据源数组

Inner tableview datasource array

var orderData:[ODOrderDataMaster] = [] {
        didSet {
            self.tblOrderMaster.reloadData()
            self.layoutIfNeeded()
            self.const_Height_tblOrderMaster.constant = self.tblOrderMaster.contentSize.height
            self.layoutIfNeeded()

        }
    }

问题:当我滚动主表格视图时,它未按照子项目正确调整大小.而最初显示时是正确的.

Question: When I scroll main tableview it is not correctly resized as per subitems.while initially it is showing correct.

我几乎已经尝试过所有诸如layoutIfNeeded之类的东西. layoutSubviews. setNeedsDisplay.

I have almost tried everything like layoutIfNeeded. layoutSubviews. setNeedsDisplay.

我还实现了willDisplayCell,但没有成功.

I have also implemented willDisplayCell but not success.

我认为问题是当我重新加载内部tableview时.以及完成重新加载后,如何通知主tableview以及如何更新内部tableview的高度常数

I think issue is when I reload inner tableview. and when it is finished reload how can i inform to main tableview and how can i update height constant of inner tableview

请让我知道是否有人需要更多信息.

Please let me know if anyone need more info.

我很感谢您的帮助或建议

I appreciate any help or suggestion

推荐答案

我尝试了几乎所有可能的解决方案,但每个解决方案都有一些问题.因此,我找到了另一种解决方法来解决此问题,它不能被称为完美解决方案,但是它对我有用.

I have tried almost all the possible solutions but each has some problems. So I have found another workaround to fix this It can not be called perfect solution but yes it works for me

在表格视图单元格中,我添加了一个隐藏标签,即顶部:0,底部:0,左侧:0和右侧:0

In tableview cell, I have added One Hidden Label which is Top : 0 , Bottom : 0 , left : 0 and right : 0

现在

    // This is temp label which help to calulate height to tableview
    if let orderMaster = restObj.orderDataMaster {

        var tempText = ""
        for orderMasterObject in orderMaster {

            tempText += (orderMasterObject.item?.name ?? "") + "\n" + "  "

            if let subItems = orderMasterObject.masterSubitem {

                let result = subItems.reduce("", { (result, subItem) -> String in
                    return result +  (subItem.subitem?.name ?? "") + ","
                })
                tempText += result + "\n" + "  "

            }
            tempText += "\n"
        }
        cell.lblTemp.text = tempText
    }

    self.view.layoutIfNeeded()
    cell.setNeedsLayout()
    cell.delegate = self 

这里要做的是,内部tableview的单元格中的标签会随着文本的增长而增加.因此,在我的情况下,该temp标签具有相同的文本(masterSubitem),简而言之,我已经在该标签中复制了内部tableview单元,因为我们提供了所有四个约束来进行标签,因此当它增长时,tableview单元将随之增长.

What here done is, Inner tableview's cell has labels which grow as per text. So That temp label has same text (masterSubitem) in my case, in short I have replicated inner tableview cell in that label as we have provided All Four constrains to label so when it will grow our tableview cell will grow along with it.

希望这对面临相同问题的人很有帮助.如果你们中的任何人都可以发布其他可行的解决方案,我会很乐意接受它:)

Hope it is helpful to someone who is facing the same issue If any of you can post another working solution I will happily accept it :)

这篇关于TableView TableTable内部的自动尺寸表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆