gpt4 book ai didi

ios - 编辑自定义 UITableViewCell 时不出现插入/删除编辑控件

转载 作者:行者123 更新时间:2023-12-01 18:43:18 27 4
gpt4 key购买 nike

我已经对此进行了研究,但似乎还没有找到解决方案。我有一个自定义 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com