默认情况下,我有一个带有多个标签的自定义单元格(用户单击该单元格时会看到所有内容)。我正在更改单元格的高度和cell.clipToBounds = true以隐藏标签。但这不适用于可编辑的细胞。当我开始向左滑动单元格时,隐藏的内容就会出现。我注意到,如果我首先展开单元格、折叠和滑动,它就会工作,但如果我滑动另一个单元格,它就会停止工作。有什么建议吗?当手机被滑动时,如何隐藏它?

发布于 2015-04-19 20:49:37
将标签添加到数组中。
将hidden设置为true
let labels = [firstLabel, secondLabel, etc.…]
for label in labels {
label.hidden = true
}发布于 2015-05-04 08:01:13
我认为这是一个setEditing在动画和编辑过程中覆盖或忽略clipsToBounds的bug。
解决方案:
您应该隐藏当表“关闭”时不应该出现的所有元素,并在展开表时取消它们。
1)创建一个UITableViewCell方法来隐藏/取消隐藏所需的所有元素:
func hideAll(hide: Bool) {
myLabel.hidden = hide
myUISwitch.hidden = hide
}2)初始化单元格时调用上述方法。通常在单元格内的某个地方,在初始化器上调用,或者在cellForRowAtIndexPath上调用。我检查cell.bounds.height以确定单元格是否已展开:
//cell init code
//I'm calling this method from inside the cell, so I use self here.
//If you're calling this method from your controller you should
//target the cell with cell.bounds.height, as well as targeting the
//cell to call the method: cell.hideAll(Bool)
var hidden = (self.bounds.height < 80) //change this number depending on your collapsed and expanded cell heights
hideAll(hidden)3)此时(如果上述方法是在cellForRowAtIndexPath内部或在单元初始化程序上调用的),则在创建单元格时,以及每次单元格扩展时,都会调用您的方法。但不是当细胞收缩(更多关于这一点的未来)。因此,在editingStyleForRowAtIndexPath中添加一些代码
override func tableView(tableView: UITableView,
editingStyleForRowAtIndexPath indexPath: NSIndexPath)
-> UITableViewCellEditingStyle{
if let cell = tableView.cellForRowAtIndexPath(indexPath) as? CustomTableViewCell {
cell.hideAll(cell.bounds.height < 80) //calls hideAll with a Bool argument, depending on height of cell
}
return .Delete
}只在您滑动的单元格上调用editingStyleForRowAtIndexPath,而不调用任何委托方法(如heightForRowAtIndexPath ),因此我认为这是隐藏视图项的最佳位置。
,好的,这就是解决办法。现在原因:
当您想隐藏这些单元格时,真正的问题是时来隐藏它们。首先,您必须了解tableViewCell所经历的循环。
我假设你在扩展/收缩heightForRowAtIndexPath上的细胞。在语义上是完全合理的。但是假设您有3个单元格(都是可扩展的)。每当调用println()、didSelectRowAtIndexPath和heightForRowAtIndexPath时,我都在做一个didSelectRowAtIndexPath。在下面的示例中,我点击了第一行,然后点击了第二行,然后点击了第三行。在控制台上观察结果:
>>>> called didSelectRow at index 0
???? called heightForRow at index 0
???? called heightForRow at index 1
???? called heightForRow at index 2
???? called heightForRow at index 0
==== called cellForRow at index 0
???? called heightForRow at index 0
>>>> called didSelectRow at index 1
???? called heightForRow at index 0
???? called heightForRow at index 1
???? called heightForRow at index 2
???? called heightForRow at index 1
==== called cellForRow at index 1
???? called heightForRow at index 1
>>>> called didSelectRow at index 2
???? called heightForRow at index 0
???? called heightForRow at index 1
???? called heightForRow at index 2
???? called heightForRow at index 2
==== called cellForRow at index 2
???? called heightForRow at index 2正如您所看到的,当您didSelectRowAtIndexPath时,heightForRowAtIndexPath会被多次循环调用,直到斯威夫特确信它的高度是正确的。同时,在您选择的单元上,cellForRow被称为only。
因此,如果您的单元格在选择另一个单元格时自动折叠,您必须意识到它们在折叠时没有运行任何代码(例如隐藏元素的方法),因为cellForRowAtIndexPath只在被点击的单元上调用。
可以在您的heightForRow代码中隐藏/取消隐藏,但是该代码被多次调用,实际上并不是最优的。当您需要时,应该只调用一次代码。所以最好的解决办法是在editingStyleForRowAtIndexPath内部调用它
发布于 2016-05-13 23:58:17
在UITableViewCell子类中,重写willTransitionToState(state: UITableViewCellStateMask)方法并将contentView.clipToBounds设置为true,如下所示:
override func willTransitionToState(state: UITableViewCellStateMask) {
super.willTransitionToState(state)
contentView.clipsToBounds = true
}从现在开始,它将正确地剪辑单元格的子视图。
https://stackoverflow.com/questions/29731204
复制相似问题