【问题标题】:Adding a UITextField to a UITableViewCell only shows a textField in the LAST cell将 UITextField 添加到 UITableViewCell 仅在最后一个单元格中显示 textField
【发布时间】:2012-01-12 14:39:58
【问题描述】:

我有一个包含许多单元格的表格视图。每个单元格都有自己的 UITextField。我以编程方式添加了文本字段。我希望每个 textField 在按下编辑按钮时出现。 (现在表格处于编辑模式),当再次按下时,我希望所有的文本字段都消失(离开编辑模式)。我知道我可以使用 hidden 属性来完成此操作,但我尝试在此方法中执行此操作:

    - (IBAction)editButton:(id)sender 
{
    if (self.editing) 
    {
        [self setEditing:NO animated:YES];
        [self.myTableView setEditing:NO animated:YES];
        EditButton.title = @"Edit";
        cellText.hidden = YES;  //<-- THIS IS THE CODE
    } 
    else 
    {
        [self setEditing:YES animated:YES];
        [self.myTableView setEditing:YES animated:YES];
        EditButton.title = @"Done";
        cellText.hidden = NO;  //<-- THIS IS THE CODE
    }
}

但它只显示和隐藏最后一个单元格的文本字段。我怎样才能让它显示在哪里,然后不显示每个单元格的文本字段?非常感谢提前!!!

行单元格

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
     {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


         cellText = [[UITextField alloc]init];
         [cellText setFrame:CGRectMake(190, 15, 55, 30)];
         cellText.text = @"1";
         cellText.borderStyle = UITextBorderStyleRoundedRect;
         cellText.hidden = YES;
         cellText.userInteractionEnabled = NO;
         [cell addSubview:cellText];
    }    

return cell;
}

提前致谢!! :D

【问题讨论】:

  • 您在使用 cellForRowAtIndexPath 方法的代码中犯了一些错误。请确保传递每个 textField 的标签。请发布该方法的一些代码。这将真正帮助我们解决问题。
  • 好吧!我现在会!感谢您的帮助!

标签: iphone ios uitableview


【解决方案1】:

你可以摆脱这个问题,使用这个技巧,我不确定它是否会在你的代码中造成内存泄漏。因为,它每次都会创建新的单元格。但如果你不这样做,你肯定可以使用它没有得到一些正确的方法。 ;)

- (IBAction)editButton:(id)sender 
{
 if (self.editing) 
        {
            [self setEditing:NO animated:YES];
            [self.myTableView setEditing:NO animated:YES];
            EditButton.title = @"Edit";

        } 
        else 
        {
            [self setEditing:YES animated:YES];
            [self.myTableView setEditing:YES animated:YES];
            EditButton.title = @"Done";

        }
        [self.myTableView reloadData];
}

重新加载 TableView 后,检查 cellForRowAtIndexPath 中的条件,或者将 self.editing 的值传递给 TextField 使其隐藏/显示。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];


    cellText = [[UITextField alloc]init];
    [cellText setFrame:CGRectMake(190, 15, 55, 30)];
    cellText.text = @"1";
    cellText.borderStyle = UITextBorderStyleRoundedRect;
    cellText.hidden = YES;
    cellText.backgroundColor = [UIColor redColor];
    cellText.userInteractionEnabled = NO;
    [cell addSubview:cellText];

    cellText.hidden=!self.editing;

    return cell;
}

【讨论】:

  • 非常感谢您的帮助!我插入了你的代码,我仍然有同样的问题......它只显示然后隐藏最后一个单元格的文本字段。还有什么我可以做的吗?再次感谢!! +1
  • 这是我的工作代码....任何你可以尝试打印 self.editing / BOOL 的值,它应该相应地改变。
  • 嗯,这很奇怪。当我删除 [self.myTableView reloadData];方法, textField 根本不会出现或消失。当我确实有该方法时,tableView 进入和退出编辑模式时没有动画,但是当我删除它时,动画很好。谢谢!
  • 是的,每次都需要重新加载tableView。而且由于您重新加载表格视图,每次它都会创建新的单元格并且不会有动画,这是上面代码的缺点。但是无论如何您都可以使用上面的代码 sn-p 解决您的问题。
【解决方案2】:

虽然您为每个单元格创建了一个文本字段,但您只保留了对名为 cellText 的 ivar 中最后一个字段的引用。这就是您显示/隐藏唯一文本字段的原因。

我建议您在切换编辑模式时重新加载表格,并在tableView:cellForRowAtIndexPath: 中设置文本字段的可见性。

哦,你应该在将cellText 添加为子视图后释放它。否则你正在泄漏内存。并且强烈建议您将子视图添加到UITableViewCell 内容视图,而不是直接添加到单元格。

【讨论】:

  • 感谢您的帮助!!另外,您说我保留了对 ivar 中最后一个 textField 的引用。我怎么能解决这个问题?另外,我尝试添加 [myTableView reloadData];方法到我的 editButton 方法,当我尝试将表格设置为编辑模式时,它只是隐藏了我的动画。它让一切变得生涩!再次感谢!
【解决方案3】:

试试这个代码

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}



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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UITextField * cellText = [[UITextField alloc] initWithFrame:CGRectMake(1, 1, 100, 30)];
cellText.tag = 1;
cellText.textColor = [UIColor darkTextColor];
//cellText.numberOfLines = 0;
cellText.font = [ UIFont fontWithName: @"Helvetica-Bold" size: 12.0 ] ; 
cellText.backgroundColor = [ UIColor clearColor ] ;
cellText.text = @"123";
    cellText.hidden = YES;
[cell.contentView addSubview:cellText];
[cellText release];
cellText =nil;

// Set up the cell...


return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
// Detemine if it's in editing mode
  UITextField *cellText = (UITextField *)[[aTableView cellForRowAtIndexPath:indexPath] viewWithTag:1];
if (!self.editing) 
{
    [self setEditing:NO animated:YES];
    [self.tableView setEditing:NO animated:YES];
   // EditButton.title = @"Edit";

    cellText.hidden = YES;  //<-- THIS IS THE CODE
} 
else 
{
    [self setEditing:YES animated:YES];
    [self.tableView setEditing:YES animated:YES];
  //  EditButton.title = @"Done";
    cellText.hidden = NO;  //<-- THIS IS THE CODE
}

return UITableViewCellEditingStyleNone;
}

你好朋友,这段代码对我来说很好用,相信你也过得愉快

【讨论】:

  • 我认为这段代码对你有帮助。我的应用中有类似的代码,它运行良好,一切顺利
  • 我绑定了这段代码......但是它仍然只显示最后一个文本字段!感谢您的帮助。有什么我可能做错了吗?我所有的代码都是正确的!
【解决方案4】:

这其实很正常。根据 addSubview 下的 Apple 文档:

视图只能有一个超级视图。如果视图已经有一个超级视图并且 那个view不是receiver,这个方法去掉了之前的 在将接收器设置为新的超级视图之前的超级视图。

所以它会不断地从单元格中添加和删除它,直到它到达最后一个。

【讨论】:

  • 显然,每个新单元格都会创建一个文本字段。
  • 哦,对了,我想我想既然它初始化到同一个指针,它仍然是同一个指针,只是本质上是“重置”,但现在我再看一遍(尤其是另一种方法)我明白。在这种情况下,你的答案肯定是正确的 =D。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-22
  • 1970-01-01
  • 1970-01-01
  • 2014-08-02
相关资源
最近更新 更多