2011-03-28 57 views
0

我有一個有十幾行的UITableView,每個都包含一個UITextField。 默認情況下的UITextField包含一個佔位符值「添加值」,如果用戶之前沒有編輯的文本字段:使用UIButton清除UITableView中的所有UITextField更改

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(158, 6, 148, 24)]; 

NSString *strReplacement = [valueArray objectAtIndex:indexPath.row]; 

if (([strReplacement length] != 0) { 
    textField.text = strReplacement; 
} else { 
    textField.placeholder = @"Add Value"; 
} 

textField.delegate = self; 
[cell addSubview:textField]; 
[textField release]; 

到目前爲止好。

我也在UITableView的頁腳添加了一個UIButton。 我想要的是當用戶單擊UIButton時清除所有編輯的值並刷新UITableView中的所有UITextFields。

我可以很容易地從valueArray中移除所有對象,但我無法弄清楚如何刷新所有UITableView單元以反映更改。

任何幫助表示讚賞。 LQ

回答

0

經過幾個小時的反覆試驗,我想出了這個解決方案。 UIButton「全部清除」調用以下方法:

- (IBAction)clearValues:(id)sender { 

    // count the number of values in the array (this is the same as the number of rows in the table) 
    int count = [valueArray count]; 

    // remove all values from the array (deletes any user added values): 
    [self.valueArray removeAllObjects]; 

    UITableViewCell *cell; 
    UITextField *textField; 

    // loop through each row in the table and put nil in each UITextField: 
    for (NSUInteger i = 0; i < count; ++i) { 

     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0]; 
     cell = [self.wordsTableView cellForRowAtIndexPath:indexPath]; 

     // NOTE: Make sure none of your tags are set to 0 since all non-tagged objects are zero. 
     // In table construction, your textFieldTags should be: textField.tag=indexPath.row+1; 

     textField = (UITextField*)[cell viewWithTag:i+1]; 
     textField.text = nil; 

    } 

} 
2

我相信你要找的是什麼

[tableView reloadData]; 
+0

當我按下UIButton時,我清除了valueArray中的值(並且驗證了值被清除),但替換值仍然在reloadData之後填充UITextFields。 – 2011-03-28 15:59:46

+0

這很奇怪。你可能想在'cellForRowAtIndexPath:'方法中放置一個斷點,並在'reloadData'後面查看'valueArray'的內容。 – filipe 2011-03-28 16:08:09

+0

- (IBAction爲)clearReplacements:(ID)發送方{\t \t \t \t \t \t \t \t \t \t \t \t \t [self.valueArray removeAllObjects]; \t [myTableView reloadData]; \t }不重新加載表格。使用斷點代碼只是在reloadData之後停止。 – 2011-03-28 16:28:11

0

菲利佩的解決方案還應該工作,但是,調用reloadData應該儘可能避免因調用該方法具有較高的性能開銷。

你需要一些類,有兩個的UIButton實例的引用以及在的UITextField,你必須在屏幕上/表中的所有實例。聽起來像你的UITableView控制器子類的完美工作!

在你上面的代碼,你爲什麼不還添加每個的UITextField您創建一個NSArray的生活在你的的UITableView控制文本字段的?然後,當用戶按下的UIButton,動作可以在你的控制器類,它通過所有在的NSArray的UITextField元素每個實例的text屬性設置爲@""循環調用一些方法。

警告:如果您打算重用的細胞,那麼你可能要確保控制器的的NSArrayUITextFields被正確地更新。

+0

我對每個UITextField元素都使用了tag屬性,但我不清楚如何在單獨的方法中解決它們? – 2011-03-28 16:05:59

1

您的解決方案感覺很奇怪。菲利普的權利,正確的方法是[wordsTableView reloadData],這將導致tableView:cellForRowAtIndexPath:被稱爲每個可見的單元格。該方法在您滾動瀏覽表時也會調用,因此如果reloadData無法正常工作,您可能最終會遇到錯誤,並且數據在更改和滾動時無法正確更新。在你的clearValues方法中,你通過調用tableView:cellForRowAtIndexPath:來做同樣的事情。

我認爲真正的問題在於tableView:cellForRowAtIndexPath:的實現。該方法通常有2個部分。首先,創建或回收電池的東西,如獲得一個參考:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; 
} 

內部的if語句通常是你應該添加子視圖到您的唯一的地方。如果dequeueReusableCellWithIdentifier:返回一個單元格,它應該已經有子視圖。

然後,在if語句之後,填充或更新子視圖的內容。原始代碼的問題在於它填充文本字段並將其添加爲子視圖,假定單元格中沒有文本字段。所以,你的tableView:cellForRowAtIndexPath:看起來應該更像是這樣的:

int textFieldTag = 100; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; 
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(158, 6, 148, 24)]; 
    [textField setTag:textFieldTag]; 
    [textField setDelegate:self]; 
    [cell addSubview:textField]; 
    [textField release]; 
} 

UITextField *textField = [cell viewWithTag:textFieldTag]; 
NSString *strReplacement = [valueArray objectAtIndex:indexPath.row]; 

if (([strReplacement length] != 0) { 
    textField.text = strReplacement; 
} else { 
    textField.placeholder = @"Add Value"; 
} 

看起來你可以將文本框的標籤值被設定爲行號,想必這樣你就可以在UITextFieldDelegate使用它。這也可能導致錯誤,就像第1行的單元格被dequeueReusableCellWithIdentifier:回收併成爲第12行一樣,它將具有意外的標記值。即使它現在沒有發生,它仍然是一個等待發生的錯誤,並且對於疑難解答將會非常棘手。

+0

好的謝謝。我會看看電池結構。 – 2011-04-01 18:02:29