2
votes

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

2

2 Answers

4
votes

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.

2
votes

"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];