【问题标题】:Enable all textfields in UITableViewCell when in Edit mode在编辑模式下启用 UITableViewCell 中的所有文本字段
【发布时间】:2012-03-21 17:44:17
【问题描述】:

我有一堆带有标签和文本框的自定义UITableViewCells。我禁用了文本框,但我想这样做,所以当用户点击编辑按钮时,它将使文本框可编辑。我该怎么做才能启用UITableView 中的所有UITextFields

我有

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.navigationItem setHidesBackButton:editing animated:YES];

    if (editing) {

    }
}

但无法在其中添加文本框启用,因为我无权访问所有文本字段。我是否需要添加代码来抓取所有单元格并循环遍历它们并启用文本字段?

【问题讨论】:

  • 我相信您必须遍历单元格并启用所需的文本字段。

标签: iphone objective-c ios5 uitableview


【解决方案1】:

我会通过在setEditing:animated: 方法中在UITableViewDelegate 上设置isEditing BOOL 并在值更改时仅更新可见单元格来做到这一点。

NSArray *visibleCells = [myTable visibleCells];
for (MyTableViewCell *cell in visibleCells)
    cell.textField.enabled = isEditing;

然后,再次使用您的UITableViewDelegate,更新出现在tableView:willDisplayCell:forRowAtIndexPath: 中的新单元格

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.textField.enabled = isEditing;
}

【讨论】:

    【解决方案2】:

    编辑UITableViewCell 的子类并在子类的viewDidLoadinit 方法中注册您的实例以获得编辑通知:

    [[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(disableTextBox) name:@"EditingIsEnabled" object:nil];

    并实现一个名为disableTextBox 的方法,该方法禁用该单元格的文本框。

    然后在您的setEditing:animated 方法中,在您要开始编辑时发布通知:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"EditingIsEnabled" object:self];

    在您的UITableViewCell 中覆盖方法dealloc 并将您自己移除为观察者,否则您将崩溃:

    [[NSNotificationCenter defaultCenter] removeObserver:self];

    如果您不使用 ARC,请务必致电 [super dealloc]。如果您使用 ARC,请不要调用 super。

    当您想禁用所有单元格时,您可以执行相同的操作,只需使用不同的名称发布通知,例如 EditingIsDisabled

    如果您需要我进一步充实代码,请告诉我。

    编辑:在这种情况下,我更喜欢 DBD 的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多