我有一个 tableView,其中的单元格包含一个 UITextField 作为每个单元格的子视图。我的问题是,当我向下滚动时,第一个单元格中的文本会在最后一个单元格中重复。如果我弄清楚原因的话我一辈子都做不到。我尝试从不同的笔尖加载单元格,将文本字段作为 ivars。 UITextFields 似乎不是问题,我认为这与 tableView 重用单元格有关。
文本字段都有一个数据源,用于跟踪文本字段中的文本,并且每次显示单元格时都会重置文本。
有任何想法吗?我真的很感激这个问题的更多答案。
更新2:
这是我的自定义单元格的代码,称为 JournalCell。非常感谢您的反馈。
我有 8 个部分,每个部分 1 行。前 7 个中有一个文本字段,最后一个是一个像按钮一样的单元格。
我正在测试按钮单元格,如果它与第 (7) 部分匹配,则返回该单元格,如果不匹配,则继续其余部分。会是这样吗?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Section %i, Row %i", indexPath.section, indexPath.row);
if (indexPath.section == 7) {
static NSString *ButtonCellIdentifier = @"ButtonCellIdentifier";
UITableViewCell *buttonCell = [self.tableView dequeueReusableCellWithIdentifier:ButtonCellIdentifier];
if (buttonCell == nil) {
buttonCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ButtonCellIdentifier] autorelease];
buttonCell.selectionStyle = UITableViewCellSelectionStyleBlue;
buttonCell.accessoryType = UITableViewCellAccessoryNone;
buttonCell.textLabel.text = sClearAll;
buttonCell.textLabel.textAlignment = UITextAlignmentCenter;
buttonCell.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8];
buttonCell.textLabel.backgroundColor = [UIColor clearColor];
}
return buttonCell;
}
static NSString *TextCellIdentifier = @"JournalCellIdentifier";
JournalCell *cell = (JournalCell *)[self.tableView dequeueReusableCellWithIdentifier:TextCellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"JournalCell" owner:self options:nil];
cell = customCell;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
cell.textField.returnKeyType = UIReturnKeyNext;
cell.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
}
switch (indexPath.section) {
case 0:
switch (indexPath.row) {
case 0:
cell.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.authorTextField = cell.textField;
self.authorTextField.text = [self.textFieldDictionary objectForKey:@"author"];
NSLog(@"Reading Author:%@", [self.textFieldDictionary objectForKey:@"author"]);
break;
}
break;
case 1:
switch (indexPath.row) {
case 0:
self.yearTextField = cell.textField;
self.yearTextField.text = [self.textFieldDictionary objectForKey:@"year"];
NSLog(@"Reading Year:%@", [self.textFieldDictionary objectForKey:@"year"]);
break;
}
break;
case 2:
switch (indexPath.row) {
case 0:
self.volumeTextField = cell.textField;
self.volumeTextField.text = [self.textFieldDictionary objectForKey:@"volume"];
NSLog(@"Reading Volume:%@", [self.textFieldDictionary objectForKey:@"volume"]);
break;
}
break;
case 3:
switch (indexPath.row) {
case 0:
self.articleTextField = cell.textField;
self.articleTextField.text = [self.textFieldDictionary objectForKey:@"article"];
NSLog(@"Reading Article:%@", [self.textFieldDictionary objectForKey:@"article"]);
break;
}
break;
default:
break;
}
return cell;
}
正如您所猜测的,问题很可能来自 UITableView 中单元格的传统重用。创建一个 NSMutableDictionary 作为类的属性,每当 UITextField 完成编辑时,将其值设置为字典中的键。
然后,在你的cellForRowAtIndexPath
方法,加载字典中对应键的值。
最理想的情况是用以下格式命名字典中的每个键[indexPath section], [indexPath row]
,因为这将有助于设置和获取值。
希望这可以帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)