基于屏幕方向IOS夫特的UITableViewCell列宽变化

问题描述 投票:2回答:3

我需要做下面的表状结构在我开发的应用程序之一。我创建使用的UITableView和我需要我的表列的宽度来改变基于屏幕方向此表。我为列的宽度设置的约束&我将在viewDidLoad中分配使用的屏幕宽度值,以这些()。

然而,我无法弄清楚当屏幕方向改变如何重新排列这些限制。我想出的是,当方向改变与I重新计算内部的&约束viewWillTransition()将被称为所谓setNeedsLayout()为表图。但是,我不能让我的表视图时,屏幕方向改变重置表格列宽。我是新来的IOS平台,任何帮助将不胜感激。

HDR_parName / HDR_parValue / HDR_minValue / HDR_maxValue

PARA1 /值1 / minvalue1 / maxvalue1

PARA2 /值2 / minvalue2 / maxvalue2

ios swift uitableview screen-orientation
3个回答
3
投票
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

    let animationHandler: ((UIViewControllerTransitionCoordinatorContext) -> Void) = { [weak self] (context) in
        // This block will be called several times during rotation, 
        // so if you want your tableView change more smooth reload it here too. 
        self?.tableView.reloadData()
    }

    let completionHandler: ((UIViewControllerTransitionCoordinatorContext) -> Void) = { [weak self] (context) in
        // This block will be called when rotation will be completed
        self?.tableView.reloadData()
    }

    coordinator.animateAlongsideTransition(animationHandler, completion: completionHandler)

}

然后的tableView数据源和委托方法将被调用,在这里可以根据的tableView帧大小设置元件尺寸。


1
投票

斯威夫特4

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)

    let animationHandler: ((UIViewControllerTransitionCoordinatorContext) -> Void) = { [weak self] (context) in
        // This block will be called several times during rotation,
        // so if you want your tableView change more smooth reload it here too.
        self?.yourTable.reloadData()
    }

    let completionHandler: ((UIViewControllerTransitionCoordinatorContext) -> Void) = { [weak self] (context) in
        // This block will be called when rotation will be completed
        self?.yourTable.reloadData()
    }

    coordinator.animate(alongsideTransition: animationHandler, completion: completionHandler)

}

0
投票

这是很简单的解决方案,你在做什么是错的。考虑一下你要全宽为你的行宽。

您没有设置任何约束单元格的宽度,而不是设置尾随约束你的tableview。它会处理所有。

如果你仍然面临着类似的问题,然后再尝试

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    self.view.layoutIfNeeded()
}

让我知道,如果你仍然面临的问题。

© www.soinside.com 2019 - 2024. All rights reserved.