更新:
感谢“Evgeny S”提供的信息,我已经能够确定删除按钮的内容是单元格背景。我在单元格init的开头调用了以下函数:
- (void) setupGradientsWithHeight:(int) the_height
{
CAGradientLayer* gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(self.bounds.origin.x,
self.bounds.origin.y,
self.bounds.size.width,
the_height);
gradient.colors = [NSArray arrayWithObjects:
(id)[UIColor colorWithRed:218.0f/255.0f
green:218.0f/255.0f
blue:220.0f/255.0f
alpha:1.0].CGColor,
(id)[UIColor colorWithRed:180.0f/255.0f
green:180.0f/255.0f
blue:184.0f/255.0f
alpha:1.0].CGColor,nil];
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
[bgview.layer addSublayer:gradient];
[self setBackgroundView:bgview];
}
通过不添加backgroundview解决了这个问题。但是,我想知道如何在保留每个单元格的渐变背景的同时解决它?
可能会发生两件事之一。当删除按钮出现时,背景视图要么被移动然后又被移回 - 或者它的大小正在减小然后再返回。 我尝试使用渐变帧的宽度组件“self.bounds.size.width”,但我仍然没有看到它会如何影响事物,因为此函数仅在单元初始化时调用一次。
原始邮件:
我在iOS6 SDK下在Xcode4中创建了一个应用程序 - 但是在新环境(Xcode5和iOS7 SDK)中加载后我遇到了问题。请注意,我已经解决了进行转换时出现的所有构建错误和警告。
我有一些带有一些细胞的桌面视图。进入编辑模式后,如果单击红色切换按钮以启动单元格的删除模式 - 动画开始并显示“删除”按钮 - 但只是短暂,然后突然重新隐藏。
我应该注意的另一个奇怪的事情是,即使删除按钮被覆盖 - 你仍然可以点击它应该的位置并删除单元格。
我试图尽可能多地取出我的“自定义代码”,我已经删除了我已经覆盖的功能,并试图尽可能地将事情恢复到原始状态 - 但无济于事。
有一次,我在输出日志中看到了一条奇怪的消息: “当我们已经有一个看起来不太好的时候,试图设置一个滑动以删除单元格。”出于某种原因,消息显示的时间更长,我似乎无法记住我当时正在做的事情。
对可能导致此问题的任何想法或建议?
答案 0 :(得分:13)
我通过找到删除按钮视图并将其置于前面来修复此问题。我在UITableViewCell子类的layoutSubviews中完成了这个。
以下是一小段代码,可以让您了解如何执行此操作:
- (void)layoutSubviews
{
[super layoutSubviews];
for (UIView *subview in self.subviews) {
for (UIView *subview2 in subview.subviews) {
if ([NSStringFromClass([subview2 class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) { // move delete confirmation view
[subview bringSubviewToFront:subview2];
}
}
}
答案 1 :(得分:10)
这是iOS 7中无数的错误之一。
出于某种原因,iOS通过删除按钮移动了backgroundView。 您可以通过继承backgroundView并实现派生视图的setFrame函数来解决这个问题,如下所示:
- (void)setFrame:(CGRect)frame
{
if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending) {
// background view covers delete button on iOS 7 !?!
[super setFrame:CGRectMake(0, frame.origin.y, frame.size.width, frame.size.height)];
} else {
[super setFrame:frame];
}
}
作为旁注:您可以通过在派生视图中继承和实现layerClass来避免使用单独的子层:
+ (Class)layerClass
{
return [CAGradientLayer class];
}
答案 2 :(得分:5)
- (void)layoutSubviews
{
[super layoutSubviews];
[self sendSubviewToBack:self.contentView];
}
答案 3 :(得分:2)
你可以使用colorWithPatternImage但对我来说使用self.layer.content更容易。 例如:
cell.layer.contents = (id)[UIImage imageNamed:@"singleRow.png"].CGImage;
这样,图像不会被拉伸,并且不必具有精确的尺寸。 这样,删除按钮不会被单元格背景图像掩盖。 按方式:这些自定义渐变背景似乎与iOS 7基本外观不匹配,您认为这可能是不批准应用程序的理由吗?
答案 4 :(得分:2)
我遇到了一个问题,我的UITableView处于编辑模式,在删除2-3行后,我收到消息“当我们已经有一个看起来不太好时,试图设置滑动以删除单元格”记录,然后除了一个单元格之外的所有单元格上的用户交互被禁用。
我通过
解决了这个问题[myTableView setEditing:NO animated:NO];
[myTableView setEditing:YES animated:NO];
每次删除后。
这种解决方法适用于我的情况。
答案 5 :(得分:1)
您可以通过为uitableview设置所需背景并设置cell.backgroudColor = [UIColor clearColor];
和cell.contentView.backgroundColor = [UIColor clearColor];
答案 6 :(得分:1)
我遇到了类似的问题并且有一个解决方法,将删除按钮放在我的单元格内容之上,但动画有点难看。完成后,按钮可见并做正确的事。
创建UITableViewCell的自定义子类并添加以下代码:
-(void)didTransitionToState:(UITableViewCellStateMask)state
{
if (state & UITableViewCellStateShowingDeleteConfirmationMask ) {
[self sendSubviewToBack:self.contentView];
}
[super didTransitionToState:state];
}
对此方法或其他指针的改进将不胜感激。
答案 7 :(得分:1)
假设一个仅限iOS7的应用程序,使用与上面的chris发布的类似的技术,我相信这里的方法:https://stackoverflow.com/a/19416870/535054将变得更清晰。
在这种方法中,您不需要对backgroundView进行子类化,对于不同的单元格可能会有所不同。
将代码放在我上面链接的答案中,在自定义表格单元格层次结构的根目录中,以及所有表格单元格(从中继承),只要他们使用 backgroundView就可以获得修复或 selectedBackgroundView 属性。
答案 8 :(得分:1)
我在更新到XCode 5.0.1后仍然看到这个问题,但是我使用了一种不同的解决方法,即在changeTitle
中将名为willBeginEditingRowAtIndexPath
的布尔值设置为NO并设置为YES如果我们没有处于行删除模式,请在editingStyleForRowAtIndexPath
中。
然后我使用该变量用titleForDeleteConfirmationButtonForRowAtIndexPath
中的一些额外空格填充按钮标题。
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0 && changeTitle) {
return @" Delete";
}
return @"Delete";
}
它对我有用,但主要的缺点是确认区域比你想象的要宽,所以可能会无意删除。
答案 9 :(得分:0)
如果您在iOS 6 SDK中启动了项目并使用了故事板,则可能是约束问题。尝试删除所有单元格约束并回退到建议。这可以解决删除按钮的问题。我在两个不同的项目中遇到了这样的问题,并且已经解决了这些问题。
答案 10 :(得分:0)
还有一个解决方法:
- (void) layoutSubviews {
[super layoutSubviews];
if ([ [ [UIDevice currentDevice] systemVersion] compare: @"7.0" options: NSNumericSearch] != NSOrderedAscending) {
if (iOS7 == YES) {
self.backgroundView.frame = CGRectMake(0, self.backgroundView.frame.origin.y,
self.backgroundView.frame.size.width, self.backgroundView.frame.size.height);
}
}
答案 11 :(得分:0)
这里有一个更好的方法,只是确保你继承UITableviewController以获得更好的性能并添加此代码
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
CGRect frame = self.tableView.frame;
if(editing){
[self.tableView setFrame:CGRectMake(0, frame.origin.y, frame.size.width, frame.size.height)];
//other codes to run if needed
}else{
[self.tableView setFrame:frame];
//other codes to run if needed
}
}