2010-10-29 58 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

这可能是因为您已加入子视图细胞视图(含标签) - 在单元格的内容查看(含标签)正在寻找子视图。这是我的问题,我解决了它。