文本字段在UITableView中出现两次
我的文本字段在UITableView中出现两次,uitableview,uitextfield,Uitableview,Uitextfield,我的TableViewController中有ageTextField属性。我将其添加到第一个部分单元格的子视图中,但我不知道为什么向下滚动时它会显示在另一个部分(#3)中 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITable
TableViewController
中有ageTextField
属性。我将其添加到第一个部分单元格的子视图中,但我不知道为什么向下滚动时它会显示在另一个部分(#3)中
- (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;
}
这是开头的截图
向下滚动后,请注意ageTextField的占位符是如何在第3节第3单元格中复制的
再次向上滚动时,第3节中单元格3的文本显示在第一个单元格中
dl.dropbox.com/u/30838210/Screen%20Shot%202012-08-07%20at%209.27.52%20PM.png
这太奇怪了,我不知道发生了什么!请帮助..当表格给您一个可重复使用的单元格时,它不关心以前在哪个部分使用过它。由于您添加了一个子视图,无论它是一个新单元格还是以前添加了子视图的单元格,因此很可能会得到多个不同类型的子视图。如果重复使用表视图单元格的内容(即,如果单元格!=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;
}
我使用传递的参数indexPath中的section属性来控制应该考虑哪个单元格。有更好的方法吗?对每个部分使用不同的标识符,如果是新单元格,则只添加额外的视图。我修复了回答中的错误,并提供了其他信息。请适当地更新您的代码。@0x141E我无意中否决了这个答案。您是否有任何可能的编辑,以便我可以撤回此投票并将其恢复为向上投票?