ios - 当文本字段在 uitableview 的单元格中时,不调用 touchesBegan

标签 ios objective-c uitableview uitextfield touchesbegan

所以我有一个文本字段,我在 .h 文件中有 UITextFieldDelegate。
我在 .m 文件中声明文本字段:

UITextField * titletextfield

我将文本字段放在表格 View 的一个单元格中。我将文本字段委托(delegate)设置为自我,
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row==0) {
    static NSString *CellIdentifier = @"Title";
    UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UILabel * label;
    if (cell == nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        label = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, 90.0, cell.frame.size.height)];
        label.text =@"sth :";
        label.font=[UIFont systemFontOfSize:14.0];
        label.textColor =[UIColor grayColor];

        titletextfield =[[UITextField alloc]initWithFrame:CGRectMake(100, 0, cell.frame.size.width-100.0, cell.frame.size.height)];
        self.titletextfield.delegate=self;

        UIView *message = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];
        message.tag = 0;
        message.backgroundColor =[UIColor clearColor];
        [message addSubview:label];
        [message addSubview:titletextfield];
        [cell.contentView addSubview:message];
    }
    return  cell;}

touch begin 从未被调用:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"123");
[self.view endEditing:YES];
[super touchesBegan:touches withEvent:event];
NSLog(@"22");

}

当我取消选中从 tableview 启用的用户交互时,开始调用 touchbegan 函数。但是,在这个 cad 中,我不能再将焦点放在文本字段上。
以前有人有同样的问题吗?谢谢!

最佳答案

这里发生的事情意味着当您启用 UITableview“用户交互启用”属性时,UITableview 的 ScrollView 会观察到所有的触摸。此时 UITableview 的 ScrollView 的所有 subview 都会获取触摸事件(在这种情况下,您的 uitextfield 也是)。所以viewcontroller 的 View 的 touchesBegan 方法永远不会调用。如果禁用 UITableview 的“用户交互启用”属性,则 UITableview 的 ScrollView 不会观察到所有触摸事件并提供给 viewcontroller 的 View 。此时将调用 touchesBegan。

关于ios - 当文本字段在 uitableview 的单元格中时,不调用 touchesBegan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28022105/

相关文章:

ios - 架构 arm64 的 undefined symbol : _YGConfigNew in libyoga. a(Yoga.o)

android - 如何使 WebView 在 react-native-webview 中可滚动?

iphone - 插入到 UITableView 的位置

ios - 边距空间 UITableView (Swift)

ios Storyboard上的可折叠/可扩展表格 View

iphone - 将 CALayer 添加到 UIView 并能够在方向更改时重绘

ios - Secure Enclave ECIES 加密数据格式是什么?

ios - Swift 4.2 - 解码相同 key 不同类型的 JSON

ios - 自动布局 View 在不同的屏幕尺寸上总是相同的尺寸

objective-c - asyncsocket "connectToHost"总是成功并且永远不会返回失败