【问题标题】:select the text field of only one custom cell in table view在表格视图中仅选择一个自定义单元格的文本字段
【发布时间】:2014-03-12 02:46:29
【问题描述】:

我在表格视图上有自定义单元格。所有自定义单元格上都有一个文本字段。文本字段以禁用状态开始。我已经对它们进行了配置,以便在长按后启用文本字段并且用户可以编辑文本字段。

但是,我希望文本字段一次只启用一个,这样用户就无法切换到其他单元格上的文本字段。所以我只想启用

这是我的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

simpleTableIdentifier = @"SimpleTableCell";

cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

        [cell.textField setEnabled:NO];

        //put in long press
        UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
        [cell addGestureRecognizer:longPressGesture];
    }
}
}

然后我们有这个:

- (void)longPress:(UILongPressGestureRecognizer *)gesture
{

if (gesture.state == UIGestureRecognizerStateBegan)
{
    // get affected cell
    cell = (SimpleTableCell *)[gesture view];

    // get indexPath of cell
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:0];

    // enabling text field
    [cell.textField setEnabled:YES];
    [cell.textField becomeFirstResponder];

}
}

如您所见,表格视图中的所有文本字段均已启用。如何仅激活所选单元格的表格视图?

谢谢。

【问题讨论】:

  • 您是否考虑过将 UILongPressGestureRecognizer 添加到 SimpleTableCell.m 中单元格的 contentView 中,而不是在您的 tableView.m 中?
  • 你使用 [tableView registerNib:[UINib nibWithNibName:@"SimpleTableCell" bundle:nil] forCellReuseIdentifier:simpleTableIdentifier]; ?

标签: ios objective-c uitableview keyboard


【解决方案1】:

您可以做的是首先隐藏 cellForRowAtIndexPath 中的所有文本字段,然后在调用长按 longPress: 方法时取消隐藏该特定单元格的文本字段,然后比较索引路径是否与长按一个相同然后取消隐藏

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

---------
if(indexPath.row == selectedIndexPathRow) {

   cell.textField.hidden = NO;   
    [cell.textField becomeFirstResponder];

}
else {

   cell.textField.hidden = YES;
---------
}

- (void)longPress:(UILongPressGestureRecognizer *)gesture
{

if (gesture.state == UIGestureRecognizerStateBegan)
{
    // get affected cell
    cell = (SimpleTableCell *)[gesture view];

    // get indexPath of cell
    selectedIndexPath = [self.tableView indexPathForCell:cell];
    selectedIndexPathRow = selectedIndexPath.row;

    [self.tableView reloadData];

}

}

【讨论】:

    【解决方案2】:

    不要在长按处理程序方法中启用您的文本字段,只需使用该方法选择单元格,然后调用 reloadData.在你的 cellForRowAtIndexPath 中,有一个 if-else 子句,如果索引路径等于所选行的索引路径,则启用文本字段,否则禁用它。

    【讨论】:

    • 问题是,有时我希望用户能够选择单元格,同时仍然禁用文本字段。这个想法是选择一个单元格将文件加载到音频播放器中,然后长按最终通过文本字段重命名文件。
    • @MScottWaller,然后创建一个您在触发长按手势处理程序时设置的属性,并在 cellForIndexPath(而不是 selectedCell)中使用它,就像我在回答中所说的那样。这样,您仍然可以通过常规点击来选择单元格,并且只能通过长按启用文本字段。
    【解决方案3】:

    更新您的NSIndexPath 创建如下:

    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:[gesture locationInView:cell]];
    

    【讨论】:

      【解决方案4】:

      所以我想出了一个简单的解决方案。我这样做是为了检查文本字段是否已经处于活动状态,如果不是,则启用其余的手势方法。如果启用了文本字段,那么我无法在任何其他单元格上触发手势。效果很好,如下所示:

      - (void)longPress:(UILongPressGestureRecognizer *)gesture
      {
      if (![cell.textField isEnabled]) {
      // only when gesture was recognized, not when ended
      if (gesture.state == UIGestureRecognizerStateBegan)
      {
          // get affected cell
          cell = (SimpleTableCell *)[gesture view];
      
          // get indexPath of cell
      
          //NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:[gesture locationInView:cell]];
          NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
          [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:0];
      
      
          [cell.textField setEnabled:YES];
          [cell.textField becomeFirstResponder];
      }
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-10
        • 1970-01-01
        • 2012-05-26
        • 1970-01-01
        • 2020-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多