4

我曾经UITableviewcellEditingstyleDelete显示按钮供用户单击以显示删除按钮(这与您在电子邮件应用程序上看到的方式相同,当用户单击编辑然后单击按钮以显示删除按钮时)。它在 ios6 中运行良好,但是当我在具有 ios 7 的设备上构建我的应用程序时,删除按钮消失了,但是当您点击删除按钮的区域时,它也可以删除。问题是用户看不到删除按钮(操作系统提供的红色按钮)。我的代码是:

- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Detemine if it's in editing mode
    if (self.editing)
    {
       return UITableViewCellEditingStyleDelete;
    }

   return UITableViewCellEditingStyleNone;
}

请帮我找到解决方案,我对iOS7环境了解不多。谢谢!

4

6 回答 6

2

感谢大家的建议,这个解决方案可以解决它,我把它变成自定义单元格

 UIImageView* backgroundImage;

//Variable for animation delete button
// Keep the ContactView in normal state
BOOL bFirstEditingCell;
BOOL bShownRedMinus;
BOOL bShownDeleteButton;
CGRect frameOfContactViewInNormal;
CGPoint centerOfCellInNormal;
UITableViewCellAccessoryType accessoryTypeInNormal;

//

 - (id)initWithCoder:(NSCoder *)decoder
{
    if (self = [super initWithCoder:decoder])
    {
        super.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"row_gradient" ]];
        self.backgroundView = backgroundImage;
        bShownDeleteButton = false;
        bShownRedMinus = false;
        bFirstEditingCell = true;
        accessoryTypeInNormal = UITableViewCellAccessoryNone;
    }
    return self;
}


- (void)willTransitionToState:(UITableViewCellStateMask)state {
[super willTransitionToState:state];
if (!isOS7()) {
    return;
}

for (UIView *subview in self.subviews) {
    //Keep normal value of contact view and cell
    if (bFirstEditingCell   ) {
        frameOfContactViewInNormal = self->contactView.frame;
        frameOfContactViewInNormal.size.width = kContactViewWidth;
        centerOfCellInNormal = subview.center;
        bFirstEditingCell = false;
    }

    if (state == UITableViewCellStateDefaultMask) {
        self.backgroundView = backgroundImage;
        subview.center = centerOfCellInNormal;
        //Set for position of speed dial image
        CGRect f = frameOfContactViewInNormal;
        if (bShownRedMinus) {
            f.size.width -= kRedMinusButtonWidth;
        }
        self->contactView.frame = f;
        bShownDeleteButton = false;
        self.accessoryType = accessoryTypeInNormal;
    }
    else if (state == UITableViewCellStateShowingDeleteConfirmationMask)
    {
        float sectionIndexWidth = 0.0;

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            sectionIndexWidth = 30.0;
        }
        else {
            sectionIndexWidth = 15.0;
        }
        CGPoint center = centerOfCellInNormal;
        subview.center = CGPointMake(center.x - sectionIndexWidth, center.y);
        self.backgroundView = nil;

        //Set width of contact name
        UIView* view = [subview.subviews objectAtIndex: 0];
        CGRect f = view.frame;
        f.origin.x = (kDeleteButtonWidth + sectionIndexWidth);
        view.frame = f;
        f = frameOfContactViewInNormal;
        f.size.width = self.frame.size.width - (kDeleteButtonWidth + sectionIndexWidth);
        self->contactView.frame = f;
        bShownDeleteButton = true;
        bShownRedMinus = false;
        accessoryTypeInNormal = self.accessoryType;
        self.accessoryType = UITableViewCellAccessoryNone;
    }
    else if (state == UITableViewCellStateShowingEditControlMask) {
        CGRect f = frameOfContactViewInNormal;
        f.size.width -= 5;
        self->contactView.frame = f;
        bShownRedMinus = true;
    }
    else if (state == 3) { //State for clicking red minus button
        CGRect f = frameOfContactViewInNormal;
        f.size.width += kRedMinusButtonWidth;
        self->contactView.frame = f;

        self.backgroundView = nil;
    }
}
}
于 2013-10-18T03:45:45.463 回答
2

It looks like one of the Bug from iOS 7. For some reason the backgroundView is moved by iOS over the delete button. You can work-around this by sub classing your backgroundView and implementing the setFrame function of your derived view like this :

UITableViewCell delete button gets covered up.


It may also happen when the accesoryView is specified and the editingAccessoryView is nil. Detailed explanation of this issue and solution is mentioned here :

UITableViewCell content overlaps delete button when in editing mode in iOS7.

于 2013-10-01T13:23:54.163 回答
1

您不需要检查 tableView 是否正在编辑...只需实现:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
       return UITableViewCellEditingStyleDelete;
}
于 2013-10-01T13:21:36.590 回答
0

更简单的解决方法

假设一个仅限 iOS7 的应用程序,其技术类似于上面 Vin 的帖子中链接的技术,我相信这里的方法:https ://stackoverflow.com/a/19416870/535054会更干净。

在这种方法中,您不需要对 backgroundView 进行子类化,这对于不同的单元格可能会有所不同。

将代码放在我上面链接到的答案中,在您的自定义表格单元格层次结构的根目录中,以及您的所有表格单元格(从它继承的)中,只要他们使用 backgroundView 或 selectedBackgroundView 属性,就可以得到修复。

从这个要点复制解决方案:https ://gist.github.com/idStar/7018104

于 2013-10-17T01:56:58.727 回答
0

消除此问题的最佳方法是在单元格中添加图像并将其设置在背面。

      UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bgImg.png"]];
      imageView.frame = CGRectMake(0, 0, 320, yourCustomCell.frame.size.height);
      [yourCustomCell addSubview:imageView];
      [yourCustomCell sendSubviewToBack:imageView];

如果您的文本将与删除按钮重叠,则实施自动布局。它会以更好的方式管理它。

可以生成另一种情况,即 cellSelectionStyle 将使用默认颜色突出显示。您可以设置高亮颜色如下

      yourCustomCell.selectionStyle = UITableViewCellSelectionStyleNone;

 Set your table cell's selection style to UITableViewCellSelectionStyleNone. This will remove the blue background highlighting or other. Then, to make the text label or contentview highlighting work the way you want, use this method in yourCustomCell.m class.

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    if (highlighted)
        self.contentView.backgroundColor = [UIColor greenColor];
     else
        self.contentView.backgroundColor = [UIColor clearColor];
}

我希望它能帮助你理解。

于 2013-10-03T13:44:41.277 回答
0

解决此问题的一个简单方法是将delete confirmation button view设为前视图。可以通过layoutSubviewscustomtableviewcell.m文件中实现委托方法来完成。

在我的情况下,我通过将以下代码添加到customtableviewcell.m文件中来解决它。根据视图在单元格中的放置方式,它可能略有不同。但它肯定会给你一个关于如何解决问题的想法。

- (void)layoutSubviews // called when a interface orientation occur ie. in our case when the '-' button clicks
{
[super layoutSubviews];

for (UIView *subview in self.subviews) { // Loop through all the main views of cell

   // Check the view is DeleteConfirmationView or not, if yes bring it into the front 
        if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) { 

            [self bringSubviewToFront:subview];// code for bring the view into the front of all subviews

        }

    }
}
于 2015-12-15T10:00:54.897 回答