2012-01-12 81 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

相關問題