2

In iOS 6 now they reuse the cells, but i dont want it too because it erases my data in the cells. the cells are all custom. They have UITextFields in them, but when i scroll up it adds another on top. heres how im registering: [SettingsTable registerClass:[TextFieldTableCell class] forCellReuseIdentifier:@"textFieldCell"];

and heres the code in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath,

TextFieldTableCell *textFieldCell = (TextFieldTableCell *)[tableView dequeueReusableCellWithIdentifier:@"textFieldCell"];
        [textFieldCell textFieldWithLabel];
        textFieldCell.textLabel.text = @"Homepage";
        textFieldCell.textLabel.backgroundColor = [UIColor clearColor];
        textFieldCell.accessoryType = UITableViewCellAccessoryNone;
        [textFieldCell sendSubviewToBack:textFieldCell.textLabel];
        textFieldCell.textField.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"Homepage"];
        textFieldCell.textField.returnKeyType = UIReturnKeyDone;
        [textFieldCell.textField addTarget:self action:@selector(dismissHomepageKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];

here is the code in the UITableViewCell subclass,

header:

    @interface TextFieldTableCell : UITableViewCell {

    UITextField *textField;

}
@property (nonatomic, retain) UITextField *textField;

-(void)fullTextField;
-(void)textFieldWithLabel;

@end

main:

`@implementation TextFieldTableCell
@synthesize textField;

-(void)fullTextField {
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 295, 43)];
    self.textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [self.contentView addSubview:self.textField];
}

-(void)textFieldWithLabel {
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(123, 0, 177, 43)];
    self.textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [self.contentView addSubview:self.textField];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(NSString*)reuseIdentifier {
    return @"textFieldCell";
}

@end`

how can i fix to only have it called one since every time i scroll it resets it to default? and if i check if something in the cell is nil, like the textfield "no index path for table cell being reused" but in 1 view controller, with the same code just different place of getting the textfields initial text, it will work the way all should

Ajay Soman
  • 1,631
  • 4
  • 19
  • 37
Maximilian Litteral
  • 3,059
  • 2
  • 31
  • 41

2 Answers2

4

See the documentation of dequeueReusableCellWithIdentifier: for iOS 6:

If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithStyle:reuseIdentifier: method.

You have registered a class with

[SettingsTable registerClass:[TextFieldTableCell class] forCellReuseIdentifier:@"textFieldCell"];

and therefore dequeueReusableCellWithIdentifier: never returns nil.

You should create the subviews of the table view cell in the initWithStyle:reuseIdentifier: method of your TextFieldTableCell class, for example:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 295, 43)];
        self.textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [self.contentView addSubview:self.textField];
    }
    return self;
}

An alternative is to implement prepareForReuse in your TextFieldTableCell class to "clean up" the cell content for reuse.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • @MaxHasADHD: That probably means that you forgot to call `[super initWithStyle:...`. See my updated answer. It worked in my test project so I hope that it helps you! – Martin R Oct 19 '12 at 05:32
  • thank you!i got it working:] i did forget it, found in another post on how to subclass uitableviewcell and saw i didnt add that line, it works good now! thank you – Maximilian Litteral Oct 19 '12 at 05:35
  • it logs "no index path for table cell being reused" twice, both after pushing the cell offscreen, but then third time it keeps it:[ know workaround this issue? – Maximilian Litteral Oct 19 '12 at 06:41
  • @MaxHasADHD: That seems to be a Apple bug, see http://stackoverflow.com/questions/12772197/what-is-the-meaning-of-the-no-index-path-for-table-cell-being-reused-message-i. – Martin R Oct 19 '12 at 06:46
2

"i dont want it too because it erases my data in the cells."

If this happens then you aren't doing it correctly. Try this pattern:

TextFieldTableCell *textFieldCell = (TextFieldTableCell *)[tableView dequeueReusableCellWithIdentifier:@"textFieldCell"];
if (textFieldCell == nil) {
  //create your cell
  //not sure how your subclass does this, but you need to alloc and init here
}
//set up your cell data
[textFieldCell textFieldWithLabel]; //What does this actually do? It might cause some of your problems
textFieldCell.textLabel.text = @"Homepage";
textFieldCell.textLabel.backgroundColor = [UIColor clearColor];
textFieldCell.accessoryType = UITableViewCellAccessoryNone;
[textFieldCell sendSubviewToBack:textFieldCell.textLabel];
textFieldCell.textField.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"Homepage"];
textFieldCell.textField.returnKeyType = UIReturnKeyDone;
[textFieldCell.textField addTarget:self action:@selector(dismissHomepageKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];
sosborn
  • 14,676
  • 2
  • 42
  • 46
  • its never nil, i added the == nil and then alloc it, and then tell it to add a full cell textfield, and put in its text, http://pastie.org/508169 – Maximilian Litteral Oct 19 '12 at 01:01
  • It is nil the first time it gets called. Put your code to create the text field inside the (cell == nil) section. – sosborn Oct 19 '12 at 02:42
  • i log it inside, code in the if (cell == nil) code never gets called ever, tried multiple tables, mulitple times. the code right now does create the textfield in that if, and every time i go to the table, its a blank call then. only regular cells will log its nil, but my custom cell won't, i also have tried it with mutliple cell types, such as a cell with a slider, or a cell with a switch – Maximilian Litteral Oct 19 '12 at 03:04
  • @MaxHasADHD - sorry, that was a cut and paste error. Take a look at the edited code. – sosborn Oct 19 '12 at 03:51
  • i didnt copy paste it, so i had it like that already. i dont know why it isnt nil, heres the exact code right now: http://pastie.org/5082256 and heres what the textFieldCell logs every time: – Maximilian Litteral Oct 19 '12 at 03:57
  • Hard to say without seeing all of the project, but I do have to ask, is there any special reason to subclass UITableViewCell? You can achieve all of that without using a subclass. – sosborn Oct 19 '12 at 05:14
  • it seemed easier by adding a couple lines in a subclass file because there are lots of areas i need a textfield cell, or a slider or a switch cells, but if i cant get 100% desired effect then i guess i could use normal cell and put the code in the cell==nil if statement. the issue with that though is, why does one view work and others do not? they are the same subclass table cell, but one works good, the other doesnt. makes no sense – Maximilian Litteral Oct 19 '12 at 05:19
  • add return statement at end after creating every cell – A_Thorne Nov 30 '13 at 10:22