ios - 从 UITableViewCell 内的 UITextField 中删除 Tap 手势

标签 ios objective-c iphone uitableview

基本上我想要实现的是在自定义 UITableViewCell 中有一个 UITextField 而这个文本字段应该只在长按时触发。因此,如果用户只在文本字段上点击一次,则单元格应触发其委托(delegate)方法 didSelectRow。

到目前为止,我在我的文本字段上添加了一个长按并删除了所有其他手势。因此,当我长按它时,我的文本字段会正确触发。但问题是,当我只是点击文本字段时,不会触发单元格并且不会调用 didSelectRow 方法。如果我在我的文本字段之外点击,则该方法被正确地称为 ofc。

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomCell";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.nameTextField.gestureRecognizers = nil;

    UILongPressGestureRecognizer *longPressTextField = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleCellTextFieldLongPress:)];
    [cell.nameTextField addGestureRecognizer:longPressTextField];

    return cell;
}

我用 UIGestureRecognizerDelegate 尝试了不同的东西,但似乎没有一个工作:(。

任何想法表示赞赏!谢谢 !

最佳答案

您应该能够通过将激活器放在单元本身上来完成此操作。因为您的UITextField仍在接受用户输入,它将阻止底层单元接收触摸。

您可以通过添加 UILongPressGestureRecognizer 来解决此问题到CustomCell对象和设置:

cell.nameTextField.userInteractionEnabled = NO;

关于ios - 从 UITableViewCell 内的 UITextField 中删除 Tap 手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26739127/

相关文章:

iphone - Facebook loginViewFetchedUserInfo 被调用两次

ios - 重新注册时如何更改pjsip注册账号?

ios - NSCFNumber rangeOfCharacterFromSet 错误解析

iphone - 从另一个选项卡重新加载 UITableView

objective-c - 对象之间的通信——将自身传递给另一个对象/Objective-C、Cocoa

ios - 如何重置/重新安装 ios 模拟器? (xcode 其他版本在工作不佳后构建;)

ios - 如何使 Storyboard中的 UILabel 和 UIButton 大写

c++ - Three.js 移植到原生代码?

objective-c - 如何(高度)压缩文件并在 iOS 上解压缩

ios - RestKit 映射了错误的对象