Question

I have a simple tableView with 20 rows. I created a subclass custom UITableview cell, and in cellforRowAtIndex, I add a textfield to the cell once every other 3 rows. When I scroll up and down text fields show up in the wrong rows. Note, I can't make UItextfield part of my custom cell, because this can be anything, checkbox, radio button, but for simplicity I chose UITextfield... What am I doing wrong?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:   (NSIndexPath *)indexPath
  {
    static NSString *CellIdentifier = @"TestCellIdentifier";
    testCell *cell = (testCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(!cell)
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
else{

    //HMMM I also tried removing it before adding it- it doesn't work neither
    for(UIView *v in cell.subviews){
        if(v.tag == 999 ){
            [v removeFromSuperview];
        }
    }

   //add UItextField to row if it's divisible by 3 
    if(indexPath.row %3 ==0){

        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(400, 10, 300, 30)];
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.font = [UIFont systemFontOfSize:15];
        textField.placeholder = [NSString stringWithFormat:@"%d",indexPath.row];
        textField.autocorrectionType = UITextAutocorrectionTypeNo;
        textField.keyboardType = UIKeyboardTypeDefault;
        textField.returnKeyType = UIReturnKeyDone;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        textField.tag = 999;

        [cell addSubview:textField];
    }
}


cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];


return cell;
}
Was it helpful?

Solution

don't use the Reusability ? in this scene,i will not use the reusability

OTHER TIPS

Reusing cells is a good thing and you should be able to do it.

You could consider removing the text field from the cell when it goes offscreen, before it is queued for reuse, in the delegate protocol method:

– tableView:didEndDisplayingCell:forRowAtIndexPath:

Knowing the row number you would know whether or not to remove the textfield.


Edited to add explanation:

On first glance your code looks OK, so I did a little test project. The problem with your original code is that you're adding the textfield to the wrong "view" -- UITableViewCells have some structure that you need to pay attention to. Look at the documentation for the UITableViewCell contentView property. It says, in part:

If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.

So the code should add to and enumerate subviews of the cell's contentView:

    for(UIView *v in cell.contentView.subviews){
        if(v.tag == 999 ){
            [v removeFromSuperview];
        }
    }
...
   textField.tag = 999;
   [cell.contentView addSubview:textField];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top