2

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

我需要做的是遍历所有文本字段并将其设置userInteractionEnabled为是。我在 `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 单元格。希望有人可以提供帮助。

干杯。

4

2 回答 2

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;
}

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

于 2012-09-27T00:50:42.893 回答
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";
  }
}
于 2012-10-18T20:40:55.460 回答