mhm*_*hsh 3 uitableview uitextfield
我有我的ageTextField
财产TableViewController
.我将它添加到第一部分单元格的子视图但我不知道为什么它在另一部分(#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;
}
Run Code Online (Sandbox Code Playgroud)
这是开头的截图..
http://dl.dropbox.com/u/30838210/Screen%20Shot%202012-08-07%20at%209.26.47%20PM.png
向下滚动后,请注意ageTextField的占位符在Section3,cell3中是如何重复的.
http://dl.dropbox.com/u/30838210/Screen%20Shot%202012-08-07%20at%209.27.28%20PM.png
再次向上滚动时,section3中的cell3文本出现在第一个单元格中.
dl.dropbox.com/u/30838210/Screen%20Shot%202012-08-07%20at%209.27.52%20PM.png
这很奇怪,我无法弄清楚发生了什么!请帮忙..
如果重用它们,则必须删除表视图单元格的内容(即,如果cell!= nil).此外,子视图应添加到表格单元格的contentView而不是单元格本身,因此在进入/退出编辑模式时,子视图会相应调整.
[cell.contentView addSubview:self.ageTextField];
[cell.contentView addSubview:self.riskTextField];
Run Code Online (Sandbox Code Playgroud)
以下代码准备单元以供重用.
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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1120 次 |
最近记录: |