简体   繁体   English

iOS编辑tableview隐藏单元格中的子视图

[英]iOS editing tableview hide subview in cell

I have a question regarding editing a UITableView. 我有一个关于编辑UITableView的问题。 I want to know how to hide a subview when the deletion control is tapped and the the delete button appears. 我想知道如何在点击删除控件并显示删除按钮时隐藏子视图。 I've figured out how to hide the subview when the delete button is tapped, but that is too late. 我已经想出了如何在点击删除按钮时隐藏子视图,但为时已晚。 I used the following code to accomplish that: 我使用以下代码来实现:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
exerciseDate = (UILabel *)[cell viewWithTag:8989];
exerciseDate.hidden = YES;

As you can see in the screenshots below, the text 'Today' and 'Yesterday' is the subview of each cell. 正如您在下面的屏幕截图中看到的,文本'Today'和'Yesterday'是每个单元格的子视图。 I want to hide this subview when the deletion control (the red round button to the left) is tapped and the delete button appears (screen shot 2). 我想隐藏这个子视图,当点击删除控件(左边的红色圆形按钮)并出现删除按钮时(屏幕截图2)。 Do I need to set up a listener for the deletion control? 我是否需要为删除控件设置监听器? If so, how would I do that? 如果是这样,我该怎么做?

Thank you in advance! 先感谢您!

屏幕截图1屏幕截图2

There's the answer to your question, and then there's what I think you really want to do but don't know it. 你问题的答案就是答案,然后就是我认为你真的想要做但却不知道的事情。

What you asked for: Subclass UITableViewCell and use the new one. 你要求的:子类UITableViewCell并使用新的。 Implement this on the class: 在课堂上实现这个:

- (void) setEditing:(BOOL)editing animated:(BOOL)animated 
{
    [super setEditing:editing animated:animated];
    UILabel *label = (UILabel *)[self viewWithTag:3]; // whatever you tag the label as
    label.hidden = editing;
}

What I would suggest you do though is the following: If you created the cell in InterfaceBuilder (or Xcode4's version thereof), change the label on the right's resizing behavior. 我建议您做的如下:如果您在InterfaceBuilder (或Xcode4的版本)中创建了单元格,请更改右侧调整大小行为的label You want it anchored to the right (default is left). 您希望它锚定在右侧(默认为左侧)。 Then when the cell resizes the content to fit the default button on there, it will push the "Today" label over to the left. 然后,当单元格resizes内容resizes以适合那里的默认按钮时,它会将"Today"标签推到左侧。

Set those Labels as properties like 将这些标签设置为属性

@property (nonatomic, retain) UILabel *subViewLabel;

and then synthesize them. 然后synthesize它们。

When you want to hide them, follow the same procedure you are following, and use 如果要隐藏它们,请按照您所遵循的相同步骤进行操作,然后使用

self.subViewLabel.hidden = YES;

Are you using a custom cell? 你在使用自定义单元吗? you can create the same effect by using UITableViewCellStyleValue1 (that controls itself your problem, and move automatically the label to left) and then by adding programmatically the UILabel to each cell in cellForRowAtIndexPath. 您可以使用UITableViewCellStyleValue1(它自己控制您的问题,并自动将标签移动到左侧),然后通过编程方式将UILabel添加到cellForRowAtIndexPath中的每个单元格来创建相同的效果。 If you are using UITableViewCellStyleSubtitle try to use UITableViewCellStyleValue1 and add the UIlabel on the bottom of the cell.textLabel property. 如果您使用的是UITableViewCellStyleSubtitle,请尝试使用UITableViewCellStyleValue1并在cell.textLabel属性的底部添加UIlabel。

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

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