UITableViewCellDeleteConfirmationControl问题

我在项目中使用以下代码:

if([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) 

这在iOS 5和6上工作正常。但是在iOS 7上它总是返回NO。

任何人都可以告诉我这是iOS 7的问题,或者我做错了什么?

谢谢!

这是我的黑客未完成的实施,供人们从中build立。

这应该适用于iOS6和iOS7。

很明显,这段代码放在你的子类UITableViewCell中。

 -(void)willTransitionToState:(UITableViewCellStateMask)state{ NSLog(@"EventTableCell willTransitionToState"); [super willTransitionToState:state]; if((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask){ [self recurseAndReplaceSubViewIfDeleteConfirmationControl:self.subviews]; [self performSelector:@selector(recurseAndReplaceSubViewIfDeleteConfirmationControl:) withObject:self.subviews afterDelay:0]; } } -(void)recurseAndReplaceSubViewIfDeleteConfirmationControl:(NSArray*)subviews{ NSString *delete_button_name = @"button_panorama_no_arrow"; for (UIView *subview in subviews) { //handles ios6 and earlier if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) { //we'll add a view to cover the default control as the image used has a transparent BG UIView *backgroundCoverDefaultControl = [[UIView alloc] initWithFrame:CGRectMake(0,0, 64, 33)]; [backgroundCoverDefaultControl setBackgroundColor:[UIColor whiteColor]];//assuming your view has a white BG [[subview.subviews objectAtIndex:0] addSubview:backgroundCoverDefaultControl]; UIImage *deleteImage = [UIImage imageNamed:delete_button_name]; UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,deleteImage.size.width, deleteImage.size.height)]; [deleteBtn setImage:[UIImage imageNamed:delete_button_name]]; [[subview.subviews objectAtIndex:0] addSubview:deleteBtn]; } //the rest handles ios7 if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationButton"]) { UIButton *deleteButton = (UIButton *)subview; [deleteButton setImage:[UIImage imageNamed:delete_button_name] forState:UIControlStateNormal]; [deleteButton setTitle:@"" forState:UIControlStateNormal]; [deleteButton setBackgroundColor:[UIColor clearColor]]; for(UIView* view in subview.subviews){ if([view isKindOfClass:[UILabel class]]){ [view removeFromSuperview]; } } } if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) { for(UIView* innerSubView in subview.subviews){ if(![innerSubView isKindOfClass:[UIButton class]]){ [innerSubView removeFromSuperview]; } } } if([subview.subviews count]>0){ [self recurseAndReplaceSubViewIfDeleteConfirmationControl:subview.subviews]; } } } 

我希望这可以帮助别人。

无论您使用该确认button做什么,都依赖于Apple视图单元的内部实现细节,Apple可以自由更改,并且在任何系统更新之后,您的解决scheme可能会停止工作。 在你的情况下,似乎Apple不再使用表格单元格中的UITableViewCellDeleteConfirmationControl类。

如果你想在iOS 7上支持你的function,你需要检查单元格的子视图层次结构是如何改变的。 其中一个可能的方法可能是当您的单元格上显示log -recursiveDescription方法,当您看到确认button时,您将看到类似于(我从日志中剥离了一些信息)的结构:

 <MyCell: frame = (0 0; 320 44); > | <UITableViewCellScrollView: frame = (0 0; 320 44);> | | <UITableViewCellDeleteConfirmationView: frame = (320 0; 82 44);> | | | <UITableViewCellDeleteConfirmationButton: frame = (0 0; 82 43.5);> | | | | <UILabel: frame = (15 11; 52 22)> | | <UITableViewCellContentView: frame = (0 0; 287 43.5);> | | | <UILabel: frame = (15 0; 270 43.5)> | | <_UITableViewCellSeparatorView: frame = (97 43.5; 305 0.5)> | | <UIButton: frame = (297 16; 8 12.5)> | | | <UIImageView: frame = (0 0; 8 12.5)> 

正如你看到现在有两个私人类处理确认UI – UITableViewCellDeleteConfirmationView和UITableViewCellDeleteConfirmationButton,你可能需要调整它们

对于iOS 8或更高版本,可以使用editActionsForRowAtIndexPath方法来定义button和操作。 参考这个链接