确定UITableViewCell是否正在从“swipe”或“self.editButton”进行编辑

时间:2022-09-07 11:08:18

I'm trying to determine whether a UITableViewCell subclass is in edit mode from a user's swipe (in which case I don't need to indent my subviews) or from the user pressing the "Edit" button associated with the UITableViewController. (In which case I do.)

我试图确定一个UITableViewCell子类是否是在编辑模式下,从用户的刷卡(在这种情况下,我并不需要缩进我的子视图),或从用户按下与相关的UITableViewController的“编辑”按钮。 (在这种情况下,我这样做。)

I know it's possible from a cell's perspective, since the self.textLabel view automatically indents properly. I have tried:

我知道从单元格的角度来看,这是可能的,因为self.textLabel视图会自动缩进。我努力了:

-(void)layoutSubviews {
   [super layoutSubviews];

   CGRect labelFrame = self.textLabel.frame;
   labelFrame.origin.x += 5;
   myCustomUILabel.frame = labelFrame;
}

But my custom label does not properly indent. (Though the self.textLabel view does?)

但我的自定义标签没有正确缩进。 (虽然self.textLabel视图可以吗?)

I would like to avoid the following:

我想避免以下情况:

  • Providing the cells with a reference to the parent table.
  • 为单元格提供对父表的引用。

  • Overriding methods in the UITableViewController class to let the cells know whether they are being edited individually or the entire table is editing.
  • 覆盖UITableViewController类中的方法,让单元格知道它们是单独编辑还是整个表正在编辑。

2 个解决方案

#1


6  

You can override willTransitionToState: in your UITableViewCell subclass. When the "Edit" button is pressed the state will be UITableViewCellStateShowingEditControlMask(=1) and when swiping it will be UITableViewCellStateShowingDeleteConfirmationMask(=2).

您可以在UITableViewCell子类中覆盖willTransitionToState :.当按下“编辑”按钮时,状态将是UITableViewCellStateShowingEditControlMask(= 1),当滑动它时将是UITableViewCellStateShowingDeleteConfirmationMask(= 2)。

#2


1  

You should not be doing the indentation manually. The UITableViewCell will do it for you!

您不应该手动进行缩进。 UITableViewCell将为您完成!

All you have to do is make sure that you add your subviews to 'contentView' of the UITableViewCell. This is the reason why self.textLabel indents properly as you have identified.

您所要做的就是确保将子视图添加到UITableViewCell的“contentView”。这就是为什么self.textLabel正确缩进的原因。

Look at the documentation of contentView property for a UITableViewCell:

查看UITableViewCell的contentView属性文档:

The content view of a UITableViewCell object is the default superview for content displayed by the cell. If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.

UITableViewCell对象的内容视图是单元格显示的内容的默认超级视图。如果要通过简单地添加其他视图来自定义单元格,则应将它们添加到内容视图中,以便在单元格进入和退出编辑模式时将它们正确定位。

#1


6  

You can override willTransitionToState: in your UITableViewCell subclass. When the "Edit" button is pressed the state will be UITableViewCellStateShowingEditControlMask(=1) and when swiping it will be UITableViewCellStateShowingDeleteConfirmationMask(=2).

您可以在UITableViewCell子类中覆盖willTransitionToState :.当按下“编辑”按钮时,状态将是UITableViewCellStateShowingEditControlMask(= 1),当滑动它时将是UITableViewCellStateShowingDeleteConfirmationMask(= 2)。

#2


1  

You should not be doing the indentation manually. The UITableViewCell will do it for you!

您不应该手动进行缩进。 UITableViewCell将为您完成!

All you have to do is make sure that you add your subviews to 'contentView' of the UITableViewCell. This is the reason why self.textLabel indents properly as you have identified.

您所要做的就是确保将子视图添加到UITableViewCell的“contentView”。这就是为什么self.textLabel正确缩进的原因。

Look at the documentation of contentView property for a UITableViewCell:

查看UITableViewCell的contentView属性文档:

The content view of a UITableViewCell object is the default superview for content displayed by the cell. If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.

UITableViewCell对象的内容视图是单元格显示的内容的默认超级视图。如果要通过简单地添加其他视图来自定义单元格,则应将它们添加到内容视图中,以便在单元格进入和退出编辑模式时将它们正确定位。