【发布时间】:2011-01-08 20:02:28
【问题描述】:
我有一个表格视图,其中的单元格包含一个 UITextField 作为每个单元格的子视图。我的问题是,当我向下滚动时,第一个单元格中的文本在最后一个单元格中重复。如果我弄清楚原因,我就无法终生。我尝试从不同的笔尖加载单元格,将 textFields 作为 ivars。 UITextFields 似乎不是问题,我认为这与 tableView 重用单元格有关。
文本字段都有一个数据源,用于跟踪文本字段中的文本,并且每次显示单元格时都会重置文本。
有什么想法吗?我真的很感激这个问题的更多答案。
更新 2: 这是我用于自定义单元格的代码,称为 JournalCell。非常感谢您的反馈。
我有 8 个部分,每个部分 1 行。前 7 个有一个 textField,最后一个是一个像按钮一样的单元格。
我正在测试按钮单元格,如果它与第 (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;
}
【问题讨论】:
-
提供一些示例代码以便我们定位问题;)
-
您在正确使用单元格时遇到了一些问题。发布您的 cellForRowAtIndexPath: 方法,以便我们帮助修复它
-
我更新了示例代码以反映我用于自定义单元格的内容。
-
有人对我发布的新代码有反馈吗?
标签: iphone cocoa-touch ios uitableview