2012-10-19 44 views
0

當我第一次選擇UITableView的任何一行時,我已經爲單元格添加了UITextField,現在我想在第二次選中行時刪除該文本字段。如何從單元格中刪除UITextField,何時選中一行?

任何建議或示例代碼將不勝感激。 謝謝!在細胞中添加文本字段

代碼:在cellForAtIndexPath方法

didSelectedRowAtIndexPath
if (indexPath.row == selectedRow) 
      { 

        numOfBottles =[[UITextField alloc] initWithFrame:CGRectMake(240,9.0f,50, 25)]; 
        numOfBottles.tag = indexPath.row; 

        [numOfBottles setBorderStyle:UITextBorderStyleNone]; 
        [numOfBottles setBackgroundColor:[UIColor clearColor]]; 
        [numOfBottles setTextColor:[UIColor whiteColor]]; 
        [numOfBottles setTextAlignment:UITextAlignmentLeft]; 
        [numOfBottles setBackground:[UIImage imageNamed:@"blue_dropdown_normal.png"]]; 
        [numOfBottles setFont:[UIFont systemFontOfSize:16.0f]]; 
        [numOfBottles setDelegate:self]; 

        NSString* quantity = [[NSString alloc] initWithString:[subtotalObj.qtyArray objectAtIndex:(indexPath.row - 1)]]; 

        [numOfBottles setText:quantity]; 
        [numOfBottles setTextAlignment:UITextAlignmentCenter]; 
        [numOfBottles setBackgroundColor:[UIColor whiteColor]]; 
        numOfBottles.keyboardType = UIKeyboardTypeDefault; 
        numOfBottles.tag = indexPath.row; 
        [cell.contentView addSubview:numOfBottles]; 
        [numOfBottles release]; 

      } 

selectedRow = indexPath.row; 
[mainTable reloadData]; 
+1

你可以試着更好地解釋你想達到的目標嗎? – shannoga

+0

「UITableView」的一行? –

+1

向我們展示您之前嘗試過的內容,例如將「UITextField」添加到單元格的代碼。 –

回答

0

我強烈建議你創建自己的UITableViewCell子類。 在這個類中添加這個UITextView對象,它的方法- (void)setSelected:(BOOL)selected animated:(BOOL)animated顯示/隱藏你的textField。

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 
    [super setSelected:selected animated:animated]; 
    [textField setHidden:!textField.hidden]; //this alternately show and hide textField 
} 
+0

我的TL給了不使用自定義UITableViewCell的指令,現在我該怎麼辦? – Blios

+0

你可以在'didSelectRowAtIndexPath'中做到這一點,在這裏你可以使用'cellForRowAtIndexPath'來獲得你的單元格,但在我看來,更好的選擇是將這些代碼封裝在專用對象中。 –

0

爲什麼不躲呢?

[yourTextField setHidden:YES]; 

或者,如果文本框是的tableview細胞的一個子視圖只是將其刪除。

[yourTextField removeFromSuperview]; 
+0

我已添加驗證碼請檢查我的問題。 – Blios

1

您可以通過給出用於填充單元格的模型對象一個整數變量來輕鬆獲得此信息。每次用戶選擇該單元格時,該變量都會加1。

然後在 - 的tableView:didDeselectRowAtIndexPath:方法(或然而它被稱爲在您的應用程序),你可以做這樣的事情:

if (selectedCellModel.selectCnt == 1) { 
    //create the text field 
} else if (selectedCellModel.selectCnt == 2) { 
    //delete the text field 
} 
相關問題