基本上我想要实现的是在自定义 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/