繁体   English   中英

动态表视图单元格和内容大小冲突以及Pintrest布局

[英]Dynamic Table View Cell & Content Size conflicting & Pintrest Layout

目前,根据我的需求,我想开发一个Pintrest布局视图。 为此, 我在同一个ScrollView上有2个tableView

标记:我没有使用collectionView,因为为此很难自定义流程布局,我也不想为此包含3rd Party框架。

查看随附的屏幕截图-

在此处输入图片说明

用2个数组填充它们,一个数组用于偶数,一个数组用于奇数项 我正在使这些tableView不可滚动根据最高的tableView增加我的scrollView的contentView的高度 两个tableView都有自定义单元格,这些单元格具有动态增加的内容(即标签)。

在我的viewDidLoad()中

self.tableViewCol1.estimatedRowHeight = 296
        self.tableViewCol1.rowHeight = UITableViewAutomaticDimension
        self.tableViewCol1.separatorStyle = UITableViewCellSeparatorStyle.None

        self.tableViewCol2.estimatedRowHeight = 296
        self.tableViewCol2.rowHeight = UITableViewAutomaticDimension
        self.tableViewCol2.separatorStyle = UITableViewCellSeparatorStyle.None

在我的DataSource方法中-

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
        {
            var cell2 = MostLikedTVCscnd()//custom Cell for 2nd Table
            var cell1 = MostLikedTVC()//custom Cell for 1st Table

            if tableView == tableViewCol1
            {
                let cell = tableViewCol1.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! MostLikedTVC
                cell.imageCol1.image = imageArr1[indexPath.row] as? UIImage
                cell.aboutLblCol1.text = labelArr1[indexPath.row] as? String//dynamic increasing label
                cell1=cell
            }
            else if tableView == tableViewCol2
            {
                let cell = tableViewCol2.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as! MostLikedTVCscnd
                cell.imageCol2.image = imageArr2[indexPath.row] as? UIImage
                cell.aboutLblCol2.text = labelArr2[indexPath.row] as? String
                cell2 = cell
            }

//changing the height constraint of table's to make the table view non scrollable
           tableView1HghtCnstrnt.constant = tableViewCol1.contentSize.height
           tableView2HghtCnstrnt.constant = tableViewCol2.contentSize.height

     //comparing the content size of table's to check which table is the tallest & adjust the height of the main ScrollView's content       
            if tableViewCol1.contentSize.height>tableViewCol2.contentSize.height
            {
                mainViewHghtCnstrnt.constant = tableViewCol1.contentSize.height+35//mainViewHghtCnstrnt :- mainScrollView's content height constraint & 35 is the padding
            }
            else if tableViewCol1.contentSize.height<tableViewCol2.contentSize.height
            {
                mainViewHghtCnstrnt.constant = tableViewCol2.contentSize.height+35
            }
            else if tableViewCol1.contentSize.height==tableViewCol2.contentSize.height
            {
                mainViewHghtCnstrnt.constant = tableViewCol2.contentSize.height+35
            }

//returning the cell
            if tableView == tableViewCol1
            {
                return cell1

            }
            else
            {
                return cell2

            }

        }

    }

但是我的问题是表格没有正确计算其内容的大小。 我进行了一些搜索,这里的一个问题回答是- 当您提供估计的RowHeight时,contentSize将被弄乱

那我有什么选择呢? 如何才能正确实现相同目标?

您可以使用tableView的contentSize属性来获取tableView所需的高度。

我已经做了一个演示来测试这一点,并且它正在按您的期望工作。

在此处输入图片说明

我的约束如下:

  1. ScrollView:

LeadingSpace到SuperView,TrailingSpace到SuperView,TopSpace到SuperView,BottomSpace到SuperView

  1. ContainerView(scrollView内的UIView):

LeadingSpace到SuperView,TrailingSpace到SuperView,TopSpace到SuperView,BottomSpace到SuperView,EqualWidth到SuperView(即scrollView)

  1. ATableView

LeadingSpace到SuperView,TrailingSpace到BTableView,TopSpace到SuperView,宽度固定点,HeightFixed点(此约束的已创建出口),BottomSpace到具有StandardSpacing和LowPriority的SuperView(250个)

  1. BTableView

LeadingSpace到ATableView,TrailingSpace到SuperView,TopSpace到SuperView,宽度固定点,HeightFixed点(此约束的已创建出口),BottomSpace到具有StandardSpacing和LowPriority的SuperView(250)

这是我的完整代码,

class ViewController: UIViewController {

@IBOutlet weak var tableViewA: UITableView!
@IBOutlet weak var tableViewB: UITableView!

@IBOutlet weak var heightConstraintBTableView: NSLayoutConstraint!
@IBOutlet weak var heightConstraintATableView: NSLayoutConstraint!

var tableViewACellCount = 50
var tableViewBCellCound = 20

override func viewDidLoad() {
    super.viewDidLoad()

    //Dont set estimatedRowHeight as it is displaying empty cell at the bottom of tableView (try playing with this uncomment estimatedRowHeight)

    //tableViewA.estimatedRowHeight = 44.0
    tableViewA.rowHeight = UITableViewAutomaticDimension

    //tableViewB.estimatedRowHeight = 44.0
    tableViewB.rowHeight = UITableViewAutomaticDimension

    //Try both methods
    self.performSelector("adjustTableViewHeight", withObject: [], afterDelay: 0.0)
    //self.adjustTableViewHeight()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    if(tableView.isEqual(tableViewA)) { //A TableView
        return tableViewACellCount
    } else { //B TableView
       return tableViewBCellCound
    }
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("cell")

    return cell!
}

func adjustTableViewHeight() {

    dispatch_async(dispatch_get_main_queue()) { () -> Void in
        self.tableViewA.layoutIfNeeded()
        self.tableViewB.layoutIfNeeded()
    }

    heightConstraintATableView.constant = tableViewA.contentSize.height
    heightConstraintBTableView.constant = tableViewB.contentSize.height
}
}

暂无
暂无

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

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