2

现在在 iOS 6 中,他们重用了单元格,但我也不想要它,因为它会擦除我在单元格中的数据。细胞都是定制的。他们里面有 UITextFields,但是当我向上滚动时,它会在顶部添加另一个。继承人如何我注册:[SettingsTable registerClass:[TextFieldTableCell class] forCellReuseIdentifier:@"textFieldCell"];

并继承代码 - (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];

这是 UITableViewCell 子类中的代码,

标题:

    @interface TextFieldTableCell : UITableViewCell {

    UITextField *textField;

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

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

@end

主要的:

`@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`

我怎么能修复它只调用一个,因为每次我滚动它都会将它重置为默认值?如果我检查单元格中的某些内容是否为零,例如文本字段“没有重复使用表格单元格的索引路径”,但在 1 个视图控制器中,使用相同的代码只是获取文本字段初始文本的不同位置,它将以这种方式工作都应该

4

2 回答 2

4

dequeueReusableCellWithIdentifier:请参阅iOS 6的文档:

如果您为指定的标识符注册了一个类并且必须创建一个新单元格,则此方法通过调用其 initWithStyle:reuseIdentifier: 方法来初始化该单元格。

您已经注册了一个课程

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

因此dequeueReusableCellWithIdentifier:永远不会返回nil

initWithStyle:reuseIdentifier:您应该在类的方法中创建表格视图单元格的子视图TextFieldTableCell,例如:

- (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;
}

另一种方法是prepareForReuse在您的TextFieldTableCell类中实现以“清理”单元格内容以供重用。

于 2012-10-19T04:54:29.637 回答
2

“我也不想要它,因为它会擦除我在单元格中的数据。”

如果发生这种情况,那么你没有正确地做到这一点。试试这个模式:

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];
于 2012-10-19T00:49:33.873 回答