2010-10-29 50 views
1

我有四個部分第一截面的tableview具有這樣前六行7列具有文本字段和最後一行具有兩個文本框泰伯維[cell.contentview viewwithtag:]被返回nil

第一節

T1
T1
T1
T1
T1
T1
T1 T2

部2

T1 T2
T1 T2 //第二行文本框被放置在第四行
T1 T2
T1 T2 //第四行文本框被放置在某些其它行
T1 T2
T1 T2
T1 T2

部3

T1 T2
T1 T2
T1 T2
T1 T2
T1 T2
T1 T2
T1 T2

部4

T1
T1
T1
T1
T1
t1
T1
T1

二&第三部分包含有兩個文本框的每個

第四節已經包含各行中的一個文本框八行我安排一個獨特的標籤,所有的文本框七行

在textFieldDidEndEditing我將內容保存到適當的索引中的數組。

如果我在滾動文本字段中輸入數據後的tableview。一些文本字段位置被改變(即)第二行中的文本字段被放置在第三行中,類似地一些行被交換。

我已創建5個小區標識符一個用於在段中的一個前六行,第二個用於在所述第一部分中的最後一行,併爲剩餘的其他部分3的小區標識符。

當我滾動有時cell.contentview viewWithtag返回標籤

零價值,我認爲這是在某些行文本框錯誤的原因。

如何糾正這個問題?

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

NSString *identifier;<bn> 
UITableViewCellStyle style = UITableViewCellStyleDefault;<br> 
BOOL selectable = NO;<br> 

switch (indexPath.section) 
{ 
    case 0: 
    { 
     if(indexPath.row < 6) 
      identifier = @"firstsectionSixRows"; 
     else 
      identifier = @"firstsectionLastRow"; 
     break; 
    } 
    case 1: 
     identifier = @"secondsection"; 
     break; 
    case 2: 
     identifier = @"thirdsection"; 
     break; 
    case 3: 
     identifier = @"fourthsection"; 
     break; 

} 

return [self createCellForIdentifier:identifier 
          tableView:tableView 
          indexPath:indexPath 
           style:style 
          selectable:selectable]; 

} 

在createCellForIdentifier方法

else if([identifier isEqualToString:@"secondsection"]) 
{ 
    int TextTag = 8 + indexPath.row * 2; 



    UILabel *lblName = (UILabel*)[cell.contentView viewWithTag:10000]; 

    lblName.text = [NSString stringWithFormat:@"G%d",indexPath.row + 1]; 

    //NSLog(@"row = %d texttag = %d",indexPath.row,TextTag);  

    UITextField *txtFirst = (UITextField*) [cell.contentView viewWithTag:TextTag]; 

    if([textFieldArray count] > 0) 
    { 
     if(![[textFieldArray objectAtIndex:TextTag] isEqualToString:@""]) 
     { 
      if(txtFirst) 
       txtFirst.text = [NSString stringWithFormat:@"%@",[textFieldArray objectAtIndex:TextTag]]; 
      else 
       NSLog(@"Textfield is nil \n tag = %d \n cell section = %d row = %d",TextTag,indexPath.section,indexPath.row); 

     } 
    } 

    UITextField *txtSecond = (UITextField*) [cell.contentView viewWithTag:TextTag + 1]; 


    if([textFieldArray count] > 0) 
    { 
     if(![[textFieldArray objectAtIndex:TextTag + 1] isEqualToString:@""]) 
     { 
      if(txtSecond) 
       txtSecond.text = [NSString stringWithFormat:@"%@",[textFieldArray objectAtIndex:TextTag + 1]]; 
      else 
       NSLog(@"Textfield is nil \n tag = %d \n cell section = %d row = %d",TextTag + 1,indexPath.section,indexPath.row); 
     } 
    } 
} 
+2

發佈您的代碼。 – 2010-10-29 15:51:54

回答

3

我有同樣的問題(奇怪的文字來了這裏和那裏,文本框的內容交換和所有)。

原來是我的問題是不斷重新創建UITableViewCells我一直在滾動UITableView。我懷疑你的桌子視圖很長,所以它不適合在一個屏幕上,所以你不能擺脫滾動。我的UITableViewCells中也有UITextFields

我發生的事情是,一旦UITextViewCell從屏幕上消失,它被釋放(這是發生在你的情況,因爲你使用的是細胞標識符,我看到)。下次它出現在屏幕上時,它將重新使用以前的文本框和文本和設置。

我最終做的是檢查單元格中創建UITextFields的數量。這些現在創建一次,並且它們在UITableViewCell期間保持靜態,也就是說,每次調用cellForRowAtIndexPath時我都不重新創建UITextFields

我想你應該注意的另一個問題是:

[UITableView cellForRowAtIndexPath:] 

..和..

[UITableViewDelegate tableView: cellForRowAtIndexPath:] 

...是兩個不同的功能,不同的行爲。我打電話是錯誤的,後來在[UIView viewWithTag:]上給了我一個零...

只是我的2歐元。

1

我建議你寫一個方法來得到一個基於indexPath的文本索引...你可能有對象的設置之間的錯誤和得到。另外...我不會像這樣瘋狂地查找數組。你應該使用一個NSDictionary和key =「{section} _ {row}」或類似的東西。

爲了快速調試,我建議你把一個斷點,看看哪些是你的數組中,你可以做到這一點與控制檯po textFieldArray

+0

我無法得到該行中該標籤的確切文本字段。當我打印單元格子視圖時,帶有不同標籤的文本字段位於該單元格內,因此[cell.content viewfortag:TextTag]返回nil – wan 2010-10-30 10:21:14

0

這可能是因爲您已加入子視圖細胞視圖(含標籤) - 在單元格的內容查看(含標籤)正在尋找子視圖。這是我的問題,我解決了它。