繁体   English   中英

禁用超级视图用户交互时,子视图用户交互

[英]subviews userinteraction when superview userinteraction disabled swift

当父视图(即UITableViewCell的userInteractionEnabled)设置为false时,我想保持子视图(在这种情况下为UITextFields)处于活动状态(可按常规方式编辑,即点击textField并出现键盘)。

进一步说明-1.从xib初始化了一个UITableViewCell。

  1. 在此单元格内,我有三个UITextField和一个UIButton。
  2. 在某些情况下,我需要禁用单元格交互(以避免didSelectRowAtIndexPath)-这部分,我没有任何问题。
  3. 步骤3之后,单元格内的所有子视图也将被禁用。
  4. 但是我需要在textFields中输入一些文本,然后点击按钮并对其进行一些操作。

到目前为止,我的工作:1.使用userInteractionEnabled并将其设置为false

myPersonalInfoExpandedCell.userInteractionEnabled = false

2.两个文本字段firstNameText和lastNameText,我启用了userInteraction并将它设置为firstNameText成为FirstResponder

 (myPersonalInfoExpandedCell as! PersonalInfoExpandedCell).firstNameText.userInteractionEnabled = true
 (myPersonalInfoExpandedCell as! PersonalInfoExpandedCell).lastNameText.userInteractionEnabled = true
 (myPersonalInfoExpandedCell as! PersonalInfoExpandedCell).firstNameText.becomeFirstResponder()

问题是,对于textFields, userInteractionEnabled无法正常工作,我怀疑userInteractionEnabled是因为TableViewCell的userInteractionEnabled为false。 becomeFirstResponder()适用于firstNameText但我需要与单元格内的所有UI元素进行交互。

我们该怎么做? 请有人帮忙吗?

要禁用cellSelection,请实现willSelectRowAtIndexPath委托方法,并为您不想选择的单元格返回nil。 您说要在某些情况下禁用选择,因此我假设您具有要禁用选择的行的indexPaths。 只需为这些indexPath返回nil即可。 请参阅以下代码。

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath == self.MyIndex) //Check for the index path for the cell you want to disable selection for
   return nil;
else
   return indexPath;
}

也可以这样做以防止单元格突出显示。

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

这样,您不必在单元格上设置userInteractionEnabled:NO。 这样,当您点击单元格时,didSelectRowAtIndexPath委托方法将不会被调用,并且您的textFields仍将被启用

编辑

以下是上述代码的快速版本

func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
if (indexPath == self.MyIndex) //Check for the index path for the cell you want to disable selection for
    return nil;
else
    return indexPath;
}


cell.selectionStyle = .None

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM