单击编辑按钮时,将UITableViewCells中的所有UITextfields设置为可编辑

时间:2012-09-26 22:44:22

标签: objective-c xcode uitableview uitextfield

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

我需要做的是遍历所有文本字段并将userInteractionEnabled设置为yes。我在`setEditing:animated method:

中完成了这个
- (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");
}
}

然后我需要以某种方式将所有textFields放回tableview单元格。希望有人可以提供帮助。

干杯。

2 个答案:

答案 0 :(得分:0)

您应该将代码放在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;
}

在此之后,你应该在编辑按钮的方法中处理编辑和完成。顺便说一下,最好使用一个视图(UIView的子类)和UIBableViewCell的XIB文件。

答案 1 :(得分:0)

当我忘记了整个指针时,意识到我有点愚蠢。

在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";
  }
}
相关问题