gpt4 book ai didi

iphone - 第一个和最后一个 UITableViewCell 在滚动时不断变化

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:05:15 26 4
gpt4 key购买 nike

我有一个 tableView,其中的单元格包含一个 UITextField 作为每个单元格的 subview 。我的问题是,当我向下滚动时,第一个单元格中的文本在最后一个单元格中重复。如果我弄清楚原因,我一辈子都做不到。我尝试从不同的 Nib 加载单元格,将 textFields 作为 ivars。 UITextFields 似乎不是问题,我认为它与重用单元格的 tableView 有关。

textFields 都有一个数据源,用于跟踪 textField 中的文本,并且每次显示单元格时都会重置文本。

有什么想法吗?我真的很感激这个问题的更多答案。

更新 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;

最佳答案

如您所料,问题很可能来自 UITableView 中单元格的常规重用。创建一个 NSMutableDictionary 作为类的属性,每当 UITextField 完成编辑时,将其值设置为字典中的键。

然后,在您的cellForRowAtIndexPath 方法中,加载字典中相应键的值。

最好用 [indexPath section], [indexPath row] 的格式命名字典中的每个键,因为这将有助于设置和获取值。

希望这对您有所帮助。

关于iphone - 第一个和最后一个 UITableViewCell 在滚动时不断变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4635856/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com