我已经对此进行了研究,但似乎还没有找到解决方案。我有一个自定义 UITableViewCell (具有各种 subview ,包括单选按钮、标签等)。当表格 View 设置为编辑时,我希望 + 和 - 插入/删除编辑控件出现在单元格的最左侧。
如果我使用标准的 UITableViewCell,这将非常有效。但是,在使用自定义单元格时,控件不会出现。有人对如何解决这个问题有任何想法吗?
下面是我的表格 View 代码的一些快照......
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.isEditing) {
if ([tableView isEqual:self.tableView]) {
if (editingStyle == UITableViewCellEditingStyleInsert) {
// ...
}
else if (editingStyle == UITableViewCellEditingStyleDelete) {
// ...
}
}
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView isEqual:self.tableView]) {
if (indexPath.row == 0) {
return UITableViewCellEditingStyleInsert;
}
else {
return UITableViewCellEditingStyleDelete;
}
}
else {
return UITableViewCellEditingStyleNone;
}
}
以及自定义表格 View 单元格代码...
- (void)awakeFromNib
{
[super awakeFromNib];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[self setNeedsLayout];
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self configureConstraints];
}
- (void)configureConstraints
{
// This is where the cell subviews are laid out.
}
最佳答案
你没有实现 setEditing:animated:
在您的自定义单元格中正确使用方法。您忘记调用 super
:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self setNeedsLayout];
}
这是一种罕见的不调用
super
的重写方法.不相关 - 在您的表格 View 代码中,不要使用
isEqual:
要比较两个 TableView ,请使用 ==
.if (tableView == self.tableView) {
您实际上确实想查看它们是否是相同的指针。
关于ios - 编辑自定义 UITableViewCell 时不出现插入/删除编辑控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39535682/