2

I use tableview with reusable cells. On each cell I have a textField with text, which I can modify. If text is empty, I delete that cell.

Lets say that we had 100 rows and we want to modify row number 1: we tap on it, give an empty string @"", scroll down to position number 50 and tap on this cell.

What now is going is that we detect tap gesture on another cell and I call method textFieldDidEndEditing: to see should I remove this cell from tableview. I use cellForRowAtIndexPath: to get the modified cell.

The problem is that there appear other cells with empty textField. I delete modified cell, but only one. I think that this is a problem with reusable cells.

Can anybody can help me with this problem?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ImageIdentyfier = @"StandardCellWithImageIdentifier";

    StandardCellWithImage *cellImage = (StandardCellWithImage *)[tableView dequeueReusableCellWithIdentifier:ImageIdentyfier];

    if(cellImage == nil) {
        cellImage = [[StandardCellWithImage alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ImageIdentyfier];
    }
    cellImage.nameLabel.delegate = self;
    Item *item  = [self.mutableFetchResults objectAtIndex:indexPath.row];
    cellImage.nameLabel.text = item.itemText;
    cellImage.infoLabel.text = item.itemInfo;
    cellImage.checkbox.userInteractionEnabled = YES;
    cellImage.nameLabel.userInteractionEnabled = NO;
    if(item.itemIsChecked.boolValue == YES) {
        cellImage.checkbox.tag = indexPath.row;
        [cellImage.tapGesture addTarget:self action:@selector(didSelectedImageAtIndexPath:)];
        cellImage.checkbox.image = [UIImage imageNamed:@"checkbox-checked.png"];
    } else {
        cellImage.checkbox.tag = indexPath.row;
        [cellImage.tapGesture addTarget:self action:@selector(didSelectedImageAtIndexPath:)];
        cellImage.checkbox.image = [UIImage imageNamed:@"open-checkbox.png"];
    }
    return cellImage;
}
edzio27
  • 4,126
  • 7
  • 33
  • 48
  • 1
    Please post some code. But anyways best idea is to keep Model(Data of text field) and View(Cell with textfield) separate, keeping model up to date with data change, and every time loading cell from model. – Mohammad Feb 06 '13 at 09:47
  • Generic comments: try to drop nameLabel.delegate and using tag and tapGesture. You will have problems with each of those, try using didSelectRowAtIndexPath and cellForRowAtIndexPath to get same functionality – JOM Feb 06 '13 at 10:50

2 Answers2

1

When you scroll from row 1 to row 50, already existing cells are reused - including your cell with empty textField. That is why you see it several times and why your delete routine removed only one instead of all.

Sounds like your cell creation at cellForRowAtIndexPath method needs fixing to make sure empty textfield is not automatically copied to recycled cells. Without seeing any code, this exercise is left to you.

Looked at code, thanx. Could not see any "easy" fix, so proposing that you should avoid the problem. So instead of checking cell taps, maybe you should check list scrolling.

The problem you have exists only because cell, which was being edited, was recycled due user scrolling the list. Therefore remove the problem by a) don't let user to scroll while editing text or b) stop text edit when user starts scrolling.

JOM
  • 8,139
  • 6
  • 78
  • 111
  • Thanks JOM, can you sugest me how can i fixed problem with automatically reused textField? – edzio27 Feb 06 '13 at 10:09
  • It was very helpfull and it solution with resignFirstResponder while scrolling made my code to work. Thanks! – edzio27 Feb 06 '13 at 12:40
1

when your textFieldDidEndEditing is invoked finish , you should check whether the text is "" if it is "" I think you should delete it from the dataSource and then reloadData

your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method should write like this :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    XXXXXXXCell *cell = [tableView dequeueReusableCellWithIdentifier: kIdentifier];
    if (cell == nil) {

        //Init cell, only init

    }

    //Setup the cells detail, such as the textField.text and so on

    return cell;

}
Guo Luchuan
  • 4,729
  • 25
  • 33