【问题标题】:Setting all the UITextfields within the UITableViewCells to editable when edit button is clicked单击编辑按钮时将 UITableViewCells 中的所有 UITextfields 设置为可编辑
【发布时间】:2012-09-18 15:50:05
【问题描述】:

我在单元格中嵌入了文本字段,并且我有一个编辑按钮,该按钮应该触发单元格进入编辑模式,我可以在其中编辑单元格中的文本。

我需要做的是遍历所有文本字段并将userInteractionEnabled设置为yes。我在 `setEditing:animated 方法中完成了这个:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{

[super setEditing:editing animated:animated];

if (editing)
{
    int tagNumber = 0;
    for (id cellTextField in [tableView visibleCells]) {
        UITextField *aField = (UITextField *)[cellTextField viewWithTag:tagNumber];
        aField.userInteractionEnabled = YES;
        tagNumber++;
    }

    self.editButtonItem.title = NSLocalizedString(@"Done", @"Done");
}
else
{
    self.editButtonItem.title = NSLocalizedString(@"Delete", @"Delete");
}
}

然后我需要以某种方式将所有这些文本字段放回 tableview 单元格。希望有人能帮忙。

干杯。

【问题讨论】:

    标签: objective-c xcode uitableview uitextfield


    【解决方案1】:

    你应该把代码放在 UITableView 的委托方法中,就像这样:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"CellIdentifier";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[CircleViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    
        UITextField *aField = (UITextField *)[cell viewWithTag:tagNumber];
        aField.userInteractionEnabled = YES;
        return cell;
    }
    

    在此之后,您应该处理编辑并在编辑按钮的方法中完成。顺便说一句,对于 UITableViewCell,最好使用带有 XIB 文件的视图(UIView 的子类)。

    【讨论】:

    • 感谢 sunkehappy 的回复。是的,这是我通过为文本字段添加标签来接近它的方式,但是它在我正在努力的编辑按钮方法中。我可以将文本字段提取到一个数组中,但我需要直接从该方法启用文本字段的用户交互。
    【解决方案2】:

    当我忘记了指针的全部意义时,我意识到自己有点愚蠢。

    在 cellForRowAtIndexPath 方法中,我将 UITextFields 添加到一个数组中(在将它们作为子视图添加到单元格之后)。然后在编辑按钮方法中,我只是使用快速枚举循环遍历 textFields 数组并从那里更改 userInteractionEnabled 属性。

    这是创建文本字段的代码:

    - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *indexPath
    {
      // Configure the cell...
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
      if (cell == nil)
      {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
      }
    
      cell.selectionStyle = UITableViewCellSelectionStyleNone;
      tf = nil;
    
      if (self.currentLevel == 0)
      {
            // create textfield
            tf = [self makeTextField:@"Some text"];          
    
            // add to subview
            [cell.contentView addSubview:tf];
    
    
            // Textfield dimensions
            tf.frame = CGRectMake(20, 12, 170, 30);
    
            //handle textFieldDidEndEditing
            tf.delegate = self;
    
            tf.userInteractionEnabled = NO;
    
            [textFieldArray addObject:tf];
      }
    
     }
    

    这是使它们可编辑的代码:

    //Edit Button
    -(void) editObject:(id)sender
    {
    
      if (editButton.title == @"Edit")
      {
        edit = TRUE;
    
        // make all textFields editable on click
        for (UITextField *textF in textFieldArray)
        {
            textF.userInteractionEnabled = YES;
        }        
        editButton.title = @"Done";
      }
      else
      {
        edit = FALSE;
    
        // make all textFields non-editable on click
        for (UITextField *textF in textFieldArray)
        {
            textF.userInteractionEnabled = NO;
        }
        editButton.title = @"Edit";
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-27
      • 2023-03-26
      • 2016-12-16
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      • 1970-01-01
      • 2014-07-24
      • 1970-01-01
      相关资源
      最近更新 更多