2012-01-12 35 views
0

我有一个包含许多单元格的表格视图。每个单元格都有自己的UITextField。我以编程方式添加了文本字段。我希望当编辑按钮被击中时,每个textField都会出现。 (现在表格处于编辑模式),再次按下时,我希望所有的textField消失(离开编辑模式)。我知道我可以使用hidden属性做到这一点,但我想在这种方法做这个:将UITextField添加到UITableViewCell只显示最后一个单元格中的textField

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

,但只显示和隐藏的最后小区的文本字段。我怎样才能将它显示在显示位置,然后不显示每个单元格的textFIeld?提前谢谢了!!!

提前ROW

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

由于CELL! :D

+1

你正在用的cellForRowAtIndexPath method.And一些代码错误,请确保传递每个textField的标签。请发布该方法的一些代码。它确实会帮助我们解决问题。 – 2012-01-12 04:31:13

+0

好的!我会吧!感谢您的帮助! – iProRage 2012-01-12 04:32:06

回答

1

你就可以摆脱这个问题,使用这一招,我不知道,好像它的code.Since中创建的内存泄漏,它创造新的小区中的每个时间。但你一定可以使用它,如果你没有得到一些正确的做法。 )

- (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到文本字段的值,这使得它隐藏/显示。

- (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; 
} 
+0

非常感谢帮助!我插入了你的代码,而且我仍然有同样的问题......它只显示然后隐藏最后一个单元格的textField。还有什么我可以做的吗?再次感谢!! +1 – iProRage 2012-01-13 00:13:54

+0

这是我的工作代码....任何如何尝试打印self.editing/BOOL的值,都应该相应地改变。 – 2012-01-13 04:12:01

+0

嗯,这很奇怪。当我删除[self.myTableView reloadData];方法,textField不会出现或消失。当我有方法时,tableView进出编辑模式时没有动画,但当我删除它时,动画很不错。谢谢! – iProRage 2012-01-17 01:54:25

0

这实际上是正常的。按照addSubview下的Apple文档:

视图只能有一个超级视图。如果视图已经有超级视图并且该视图不是接收者,则在使接收者成为新的超级视图之前,该方法移除先前的超级视图。

因此,它会继续删除它添加和删除单元格,直到它到达最后一个。

+0

显然,每个新单元格都会创建一个文本字段。 – Costique 2012-01-12 04:48:22

+0

哦,对,我想我以为自从它初始化到同一个指针,它仍然是一个“重置”本质上是相同的,但现在我再看一遍(尤其是另一种方法),我明白了。在这种情况下,你的答案肯定是正确的= D。 – 2012-01-12 04:57:15

1

尽管您为每个单元格创建了一个文本字段,但仅保留对名为cellText的伊娃尔中最后一个的引用。这就是为什么你显示/隐藏唯一的文本字段。

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

哦,你应该在将它作为子视图添加后发布cellText。否则,你正在泄漏记忆。强烈建议您将子视图添加到UITableViewCell内容视图,而不是直接添加到单元格。

+0

感谢您的帮助!另外,你说我保留对伊娃里最后一个textField的引用。我怎么能解决这个问题?另外,我试着添加[myTableView reloadData];方法到我的editButton方法,它只是隐藏我的动画,当我尝试设置表和编辑模式。它使它全是生涩的!再次感谢! – iProRage 2012-01-17 01:54:43

1

试试这个代码

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

喜的朋友此代码为我工作很好,相信你也有一个炉排一天

+0

我认为这段代码对你很有帮助。我有这样的代码在我的应用程序和它的作品,最好的 – 2012-01-13 09:27:54

+0

我绑这个代码...但它仍然只显示最后一个textField!感谢您的帮助。有什么我可能做错了吗?我拥有所有的代码! – iProRage 2012-01-15 02:56:29

相关问题