2015-04-19 14 views
3

我有一个默认情况下隐藏了多个标签的自定义单元格(用户在单击单元格时看到所有内容)。我正在改变单元格的高度,cell.clipToBounds = true以隐藏标签。但是这对编辑单元格不起作用。当我开始向左滑动单元格时,会显示隐藏的内容。我注意到它起作用,如果我首先展开单元格,折叠和滑动,但如果我滑动另一个单元格,则停止工作。 任何建议如何在单元格被滑过时隐藏它?当滑动单元格时,clipToBounds不起作用

clipToBounds not working for editable cells

+0

如果你想隐藏标签,为什么不设置'label.hidden = true'?您可以将它们放入数组或插座集合中以使其更容易。 –

+0

亚伦,对不起,我不太确定该怎么做。试图创建一个数组,将所有这些标签添加到数组中,并使用for循环将它们全部设置为隐藏,但是我收到了一堆错误。 –

+0

...你得到什么错误?请修改您的问题以显示您正在尝试的代码。 –

回答

1

将标签添加到数组中。

设置hiddentrue

let labels = [firstLabel, secondLabel, etc.…] 
for label in labels { 
    label.hidden = true 
} 
+0

我有许多不同的对象要隐藏,如UIImageViews和UISwitches,所以我选择了一种方法,而不是将它们放入数组中。此方法是细胞控制器内: 'FUNC hideAll(隐藏:BOOL){ mySwitch.hidden =隐藏 myLabel.hidden = hide' 我请在我的TVC内部'heightForRow'此方法。仍然不起作用。实际上,一旦意见被隐藏,它们就不会再出现。但是,当刷卡发生时,它们会出现。我在想,这可能是一个Xcode错误与刷卡进行编辑? –

+0

我上面错了。缺乏对tableViewCell生命周期的理解导致了错误。将发布答案。 –

0

尝试的内容查看clipsToBounds属性设置为true。通常你不应该把细胞作为视图。

斯威夫特

cell.contentView.clipsToBounds = true 

对象 -

cell.contentView.clipsToBounds = YES 
1

我认为这是与setEditing压倒一切的或忽略的动画和编辑期间clipsToBounds的错误。

解决办法:

你应该隐藏所有当表“封闭”,它不应该出现的元素,并取消隐藏当表扩大。

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 s的周期。

我假设你正在扩大/收缩heightForRowAtIndexPath上的单元格。从语义上讲,它是完全意义上的。但是让我们假设你有3个单元格(全部可扩展)。我正在做println(),只要调用cellForRowAtIndexPath,didSelectRowAtIndexPathheightForRowAtIndexPath。在下面的例子中,我点击第一行,然后是第二行,然后是第三行。观察控制台上的结果:

>>>> 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 

正如你所看到的,当你didSelectRowAtIndexPathheightForRowAtIndexPath被多次调用,循环,直到SWIFT是确保它具有所有的高度正确。同时,cellForRow仅在您选择的单元上被称为只有

所以如果你的单元格在你选择另一个单元格时自动折叠,你必须认识到它们在折叠时没有运行任何代码(比如隐藏元素的方法),因为cellForRowAtIndexPath仅在被抽头单元格上被调用。

可能隐藏/取消隐藏您的heightForRow代码,但该代码被调用了很多次,因此它并不是最优的。当你需要时,代码只能被调用一次。所以最好的解决办法IMO是调用它里面的editingStyleForRowAtIndexPath

1

在你UITableViewCell子类,覆盖willTransitionToState(state: UITableViewCellStateMask)方法和设置contentView.clipToBounds为真这样的:

override func willTransitionToState(state: UITableViewCellStateMask) { 
    super.willTransitionToState(state) 

    contentView.clipsToBounds = true 
} 

从现在开始,它就会被裁剪单元格的子视图正确。

+0

这解决了我的问题,谢谢! –

相关问题