2012-08-08 23 views
3

我在TableViewController中有ageTextField属性。我将它添加到首节电池的subciew,但我不知道为什么它在另一部分显示(#3),当我向下滚动..textField在UITableView中出现两次

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
    } 
    // Configure the cell... 
    switch (indexPath.section){ 
     case 0: 
     { 
      [cell addSubview:self.ageTextField]; 
     } 
     break; 
     case 1: 
     { 
      cell.textLabel.text = [screeningArray objectAtIndex:indexPath.row]; 
      if (indexPath.row == 0)  //no choice 
       cell.accessoryType = self.doneScreening ? UITableViewCellAccessoryNone:UITableViewCellAccessoryCheckmark; 
      else      //yes choice 
       cell.accessoryType = self.doneScreening ? UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone; 
     } 
     break; 
     case 2: 
     { 
      cell.textLabel.text = @"1 : "; 
      [cell addSubview:self.riskTextField]; 
     } 
     break; 
     case 3: 
     { 
      cell.textLabel.text = [findingsArray objectAtIndex:indexPath.row]; 
      cell.accessoryType = [[boolValuesArray objectAtIndex:indexPath.row] boolValue] ? UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone; 
     } 
     break; 
     default: 
     break; 
    } 
    cell.textLabel.font = [UIFont systemFontOfSize:14]; 
    return cell; 
} 

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    [textField resignFirstResponder]; 
    return YES; 
} 

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { 
    NSUInteger newLength = [textField.text length] + [string length] - range.length; 
    if (textField == self.ageTextField) 
     return (newLength > 2) ? NO : YES; 
    else 
     return (newLength > 7) ? NO : YES; 
} 

下面是截图开头..

http://dl.dropbox.com/u/30838210/Screen%20Shot%202012-08-07%20at%209.26.47%20PM.png

向下滚动后,注意如何ageTextField的占位符Section3中,小区3被复制..

http://dl.dropbox.com/u/30838210/Screen%20Shot%202012-08-07%20at%209.27.28%20PM.png

当再次向上滚动时,第3部分中cell3的文本出现在第一个单元格中。

dl.dropbox.com/u/30838210/Screen%20Shot%202012-08-07%20at%209.27.52% 20 PM.png

这很奇怪,我搞不清楚发生了什么!请帮助..

回答

8

如果表格单元格的内容被重用(如果单元格!= nil),则必须删除它们的内容。另外,应该将子视图添加到表格单元格的contentView而不是单元格本身,因此子视图在进入/退出编辑模式时可以适当调整。

 [cell.contentView addSubview:self.ageTextField]; 

     [cell.contentView addSubview:self.riskTextField]; 

以下代码准备要重用的单元格。

if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
} 
else 
{ 
    // Prepare cell for reuse 

    // Remove subviews from cell's contentView 
    for (UIView *view in cell.contentView.subviews) 
    { 
     // Remove only the appropriate views 
     if ([view isKindOfClass:[UITextField class]]) 
     { 
      [view removeFromSuperview]; 
     } 
    } 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    cell.textLabel.text = nil; 
} 
+0

工作完美,非常感谢.. – user1583416 2012-08-09 00:19:42

+0

我修复了一个错误在我的答案,并提供了额外的信息。请适当更新您的代码。 – 0x141E 2012-08-10 20:16:07

+0

@ 0x141E我不小心投了这个答案。是否有可能进行的编辑,以便我可以撤回此投票并将其恢复为优先权? – 2014-10-28 01:28:36

1

当表为您提供了一个可重复使用的电池也就没有在意这部分它是在以前使用的。既然你添加一个子视图是否是一个新的小区或一个以前有一个子视图添加,它的很有可能得到不止一种和各种各样的。

+0

我使用传递参数indexPath中的section属性来控制应该考虑哪个单元格。有没有更好的办法? – user1583416 2012-08-08 03:28:11

+0

为每个部分使用不同的标识符,如果是新的单元格,则只添加额外的视图。 – 2012-08-08 11:19:22