第一个和最后一个UITableViewCell在滚动时保持更改 [英] First and last UITableViewCell keep changing while scrolling

查看:95
本文介绍了第一个和最后一个UITableViewCell在滚动时保持更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tableView的单元格包含一个UITextField作为每个单元格的子视图。我的问题是,当我向下滚动,第一个单元格中的文本被复制在最后一个单元格。我不能为生活,如果我找出为什么。我尝试从不同的nibs,有textFields作为ivars加载单元格。 UITextField似乎没有问题,我认为它有关于tableView重用单元格。

I have a tableView with cells containing one UITextField as a subview for each cell. My problem is that when I scroll down, the text in the first cell is duplicated in the last cell. I can't for the life if me figure out why. I have tried loading the cells from different nibs, having the textFields as ivars. The UITextFields don't seem to be the problem, I'm thinking it has something to do with the tableView reusing the cells.

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

The textFields all have a data source that keeps track of the text within the textField and the text is reset each time the cell is shown.

任何想法?我真的很感谢这个问题的更多答案。

Any ideas? I'd really appreciate some more answers for this question.

更新2:
这是我为一个自定义单元格,称为JournalCell的代码。非常感谢您的反馈。

UPDATE 2: This is the code I have for a custom cell, called JournalCell. Really appreciate the feedback.

我有8节,每节1行。第一个7有一个textField在其中,最后一个单元格像一个按钮。

I have 8 sections with 1 row each. The first 7 have a textField in them, the last is a cell acting like a button.

我测试的按钮单元格,如果它匹配的部分),那么它返回那个单元格,如果没有,它继续其余的。这是不是?

I'm testing for the button cell, if it matches the section (7), then it returns that cell, if not, it continues to the rest. Could this be it?

- (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完成编辑,将其值设置为字典中的一个键。

As you guessed, the issue is most likely coming from the conventional reuse of cells in a UITableView. Create an NSMutableDictionary as a property of your class, and whenever a UITextField is finished editing, set the value of it to a key in your dictionary.

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

Then, in your cellForRowAtIndexPath method, load the value of the corresponding key in the dictionary.

最理想的做法是使用格式 [indexPath section],[indexPath row] 命名字典中的每个键这将有助于设置和获取价值。

It would be most ideal to name each key in your dictionary with the format [indexPath section], [indexPath row], as this will aid in the setting and getting of values.

希望这有助于。

这篇关于第一个和最后一个UITableViewCell在滚动时保持更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆