我想自定义在 tableview 单元格上执行“向左滑动”操作时显示的删除按钮。我目前设置了 UITableViewCell 的子类,但也想自定义正在显示的删除按钮。
我的目标是在滑动时放置三个按钮。
我选择了另一个实现,我在每个单元格中都使用了 UIScrollview。
http://www.teehanlax.com/blog/reproduce-the-ios-7-mail-apps-interface/
我想自定义在 tableview 单元格上执行“向左滑动”操作时显示的删除按钮。我目前设置了 UITableViewCell 的子类,但也想自定义正在显示的删除按钮。
我的目标是在滑动时放置三个按钮。
我选择了另一个实现,我在每个单元格中都使用了 UIScrollview。
http://www.teehanlax.com/blog/reproduce-the-ios-7-mail-apps-interface/
接受的答案在 iOS 7 上不起作用,因为现在 UITableViewCellContentView 介于两者之间。所以子视图循环现在应该看起来像这样(如果你也想支持旧的 iOS 版本,请使用当前接受的 iOS 6.1 答案-)
for (UIView *subview in self.subviews) {
for (UIView *subview2 in subview.subviews) {
if ([NSStringFromClass([subview2 class]) rangeOfString:@"Delete"].location != NSNotFound) {
// Do whatever you want here
}
}
}
这可能会帮助你。
- (void)willTransitionToState:(UITableViewCellStateMask)state
{
[super willTransitionToState:state];
if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
{
for (UIView *subview in self.subviews)
{
if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"])
{
UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
[deleteBtn setImage:[UIImage imageNamed:@"arrow_left_s11.png"]];
[[subview.subviews objectAtIndex:0] addSubview:deleteBtn];
}
}
}
}
参考自:
-(void)willTransitionToState:(UITableViewCellStateMask)state{
NSLog(@"EventTableCell willTransitionToState");
[super willTransitionToState:state];
if((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
{
UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake( 320,0, 228, 66)];
[deleteBtn setImage:[UIImage imageNamed:@"BtnDeleteRow.png"]];
[[self.subviews objectAtIndex:0] addSubview:deleteBtn];
[self recurseAndReplaceSubViewIfDeleteConfirmationControl:self.subviews];
[self performSelector:@selector(recurseAndReplaceSubViewIfDeleteConfirmationControl:) withObject:self.subviews afterDelay:0];
}
}
上面的解决方案不适用于 iOS 7,在- (void)willTransitionToState:
,删除按钮不在视图层次结构中,所以我无法操作任何东西。我最终在- (void)didTransitionToState:
. 下面的示例专门针对当我的单元格在顶部有一些间距时,我正在更改删除按钮的框架。如果你想自定义删除按钮,你可以在删除按钮顶部添加一个视图或者用你自己的 UIButton 替换它
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//your own stuff
//for some reason, editingAccessoryView cannot be nil and must have a non-CGRectZero frame
self.editingAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
}
return self;
}
- (void)didTransitionToState:(UITableViewCellStateMask)state
{
[super didTransitionToState:state];
if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
{
UIView *deleteButton = [self deleteButtonSubview:self];
if (deleteButton) {
CGRect frame = deleteButton.frame;
frame.origin.y += defined_padding;
frame.size.height -= defined_padding;
deleteButton.frame = frame;
}
}
}
- (UIView *)deleteButtonSubview:(UIView *)view
{
if ([NSStringFromClass([view class]) rangeOfString:@"Delete"].location != NSNotFound) {
return view;
}
for (UIView *subview in view.subviews) {
UIView *deleteButton = [self deleteButtonSubview:subview];
if (deleteButton) {
return deleteButton;
}
}
return nil;
}