ios - 在表格 View 中仅选择一个自定义单元格的文本字段

标签 ios objective-c uitableview keyboard

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

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

这是我的代码

- (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];

}
}

如您所见,所有文本字段都在表格 View 中启用。如何仅使选定单元格的表格 View 处于事件状态?

谢谢。

最佳答案

您可以做的是首先隐藏 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];

}

}

关于ios - 在表格 View 中仅选择一个自定义单元格的文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22341014/

相关文章:

iOS7 NSKeyedArchiver 警告 : replacing existing value for key 'UITintColor' ; probable duplication of encoding keys in class hierarchy

ios - 属性文本不适用于 TableView Cell

iphone - 如何防止 UITableViewCell 被移离它自己的部分?

objective-c - 如何更改 NSOutlineView 的每个单元格的颜色或 NSTableCellView

objective-c - 复制 UIImageView 子类

cocoa-touch - 在屏幕上向后滚动时,UITableViewCell 会被重新选择

iphone - 像弹出警报 View 时一样暗淡的背景

ios - Swift:如何将 NSIndexPath 绑定(bind)到 addTarget

ios - 使用主从模板创建 TabBar Controller ?

ios - UITableViewCell 分隔线在编辑期间出现