[英]Changing the contentView when an Accessory button is tapped
I've subclassed UITableViewCell
to display a UIImage
and two UILabel
subviews. 我将
UITableViewCell
细分为显示一个UIImage
和两个UILabel
子视图。 In the view controller for the table view, in the method cellForRowAtIndexPath:
I've enabled an accessory view via setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton
. 在表视图的视图控制器中,在方法
cellForRowAtIndexPath:
我已经通过setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton
启用了附件视图。
Cells display correctly. 单元格正确显示。
When I tap on the accessory disclosure button I want to replace the two label subviews with two different label subviews. 当我点击附件公开按钮时,我想用两个不同的标签子视图替换两个标签子视图。 My approach was the following:
我的方法如下:
UITableViewCell
, inside layoutSubviews
, create the CGRect
s for the "alternate" labels, position them in the same places as the two "original" label and hide them via setAlpha:
; UITableViewCell
layoutSubviews
内部,为“ alternate”标签创建CGRect
,将它们与两个“ original”标签放置在相同的位置,并通过setAlpha:
将它们隐藏setAlpha:
The problem is I can't figure out what logic in layoutSubviews
I'd use to know whether the accessory button has been tapped. 问题是我无法弄清楚
layoutSubviews
哪些逻辑,我无法使用这些逻辑来了解是否已轻击附件按钮。 I see that in the view controller accessoryButtonTappedForRowWithIndexPath:
is called when that button is tapped and from there it seems like I would call layoutSubviews
but can't figure out how to use that fact to accomplish what I'm trying to do. 我在视图控制器中看到,在点击该按钮时会调用
accessoryButtonTappedForRowWithIndexPath:
控制器accessoryButtonTappedForRowWithIndexPath:
按钮TappedForRowWithIndexPath :,似乎从那里我将调用layoutSubviews
但无法弄清楚如何使用该事实来完成我想做的事情。
Am I going about this all wrong? 我要解决所有这些错误吗? Instead of hiding/showing
CGRect
s with alpha should I simply be creating another subclass of UITableViewCell
? 我应该只创建
UITableViewCell
另一个子类,而不是使用alpha隐藏/显示CGRect
?
When I tap on the accessory disclosure button I want to replace the two UILabel subviews with two different UILabel subviews.
当我点击附件公开按钮时,我想用两个不同的UILabel子视图替换两个UILabel子视图。
I'll do the following. 我将执行以下操作。 Create a public method in your
UITableViewCell
subclass like the following: 在您的
UITableViewCell
子类中创建一个公共方法,如下所示:
- (void)changeContentForCell;
In this method you could set the contentView
as you prefer. 在这种方法中,您可以根据需要设置
contentView
。
Then in your view controller implement 然后在您的视图控制器中实现
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
CustomCell* cCell = (CustomCell*)[tableView cellForRowAtIndexPath:indexPath];
[cCell changeContentForCell];
}
This is a simple example for change the content. 这是更改内容的简单示例。 In my opinion you don't have to use
layoutSubviews
to add views. 我认为您不必使用
layoutSubviews
添加视图。
Leave this logic in changeContentForCell
and then call setNeedsLayout
to change your layout. 将此逻辑保留在
changeContentForCell
,然后调用setNeedsLayout
更改布局。 Maybe you could have a variable that tracks the state for your cell: normal state or modified state. 也许您可以使用一个变量来跟踪单元格的状态:正常状态或修改状态。
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.