2013-09-25 125 views
6

我在tableviewcells中有UITextFields。當您滑過單元格而不是文本字段的一部分時,刪除操作按預期方式出現。如果你滑過文本框,它會阻止刪除彈出。左手手勢滑過UITextField

我該如何解決這個問題,以便您可以滑過輸入並且單元格將觸發刪除操作?

+0

你解決了這個問題嗎? –

+0

沒有。我重新設計了該問題的界面。 –

+0

嘗試添加 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath。它的作品魅力 – Lightygalaxy

回答

2

我覺得這裏的問題是,在文本字段中的觸摸與您輕掃手勢識別(可能連接到父視圖)的干擾。我在放入UIScrollView的文本字段中遇到了類似的問題。

我通過在我的UITextField上覆蓋了一個清晰的UIView來解決這個問題。然後,我爲此清除視圖分配了一個UITapGestureRecognizer,以便在用戶點擊該字段時將文本字段設置爲第一響應者。否則,被刷卡被髮送到父視圖(滾動視圖),它可以識別沒有任何問題的滑動。這有點蹩腳,但它的工作。

這種情況有點不同於你的,但我認爲這是同樣的問題。這裏是我的代碼看起來像,希望這有助於:

// UIView subclass header 
@interface LSAddPageView : UIView 

@property (weak, nonatomic) IBOutlet UITextField *textField; // Connected to the UITextField in question 
@property (strong, nonatomic) UIView *textFieldMask; 
@property (assign, nonatomic) BOOL textFieldMaskEnabled; 

@end 

// UIView subclass implementation 
@implementation LSAddPageView 

- (void)awakeFromNib 
{ 
    [super awakeFromNib]; 

    _textFieldMask = [UIView new]; 
    _textFieldMask.backgroundColor = [UIColor clearColor]; 
    [self insertSubview:_textFieldMask aboveSubview:self.textField]; 
} 

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 

    _textFieldMask.frame = self.textField.frame; 
} 

- (BOOL)textFieldMaskEnabled 
{ 
    return _textFieldMask.hidden == NO; 
} 

- (void)setTextFieldMaskEnabled:(BOOL)textFieldMaskEnabled 
{ 
    _textFieldMask.hidden = !textFieldMaskEnabled; 
} 

@end 

然後在控制器:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    _addPageView = (LSAddPageView*)self.view; 

    _maskGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapMask:)]; 
    _maskGestureRecognizer.numberOfTapsRequired = 1; 
    _maskGestureRecognizer.numberOfTouchesRequired = 1; 
    [_addPageView.textFieldMask addGestureRecognizer:_maskGestureRecognizer]; 

    self.textField.delegate = self; // Set delegate to be notified when text field resigns first responder 
} 

- (void)didTapMask:(UIGestureRecognizer*)recognizer 
{ 
    _addPageView.textFieldMaskEnabled = NO; 
    [self.textField becomeFirstResponder]; 
} 

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField 
{ 
    _addPageView.textFieldMaskEnabled = YES; 
    return YES; 
} 
1

聽起來像是你需要設置cancelsTouchesInView屬性

yourGestureRecognizer.cancelsTouchesInView = NO; 
+0

它不適合我。 –