【问题标题】:Custom table cells iOS 6自定义表格单元格 iOS 6
【发布时间】:2012-10-19 00:03:53
【问题描述】:

现在在 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 个视图控制器中,使用相同的代码只是获取文本字段初始文本的不同位置,它将以这种方式工作都应该

【问题讨论】:

    标签: objective-c xcode uitableview ios6


    【解决方案1】:

    请参阅dequeueReusableCellWithIdentifier: 的 iOS 6 文档:

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

    你已经注册了一个类

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

    因此dequeueReusableCellWithIdentifier: 永远不会返回nil

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

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

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

    【讨论】:

    • @MaxHasADHD:这可能意味着你忘了打电话给[super initWithStyle:...。请参阅我的更新答案。它在我的测试项目中工作,所以我希望它可以帮助你!
    • 谢谢!我让它工作了:] 我确实忘记了,在另一篇关于如何子类化 uitableviewcell 的帖子中找到,发现我没有添加那行,现在很好用!谢谢
    • 它记录了两次“没有重复使用表格单元格的索引路径”,两次都是在将单元格推离屏幕之后,但第三次保持它:[知道解决此问题的方法吗?
    • @MaxHasADHD:这似乎是 Apple 的错误,请参阅 stackoverflow.com/questions/12772197/…
    【解决方案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];
    

    【讨论】:

    • 它从不为零,我添加了 == nil 然后分配它,然后告诉它添加一个完整的单元格文本字段,并输入它的文本,pastie.org/508169
    • 第一次被调用时为 nil。将您的代码用于在 (cell == nil) 部分中创建文本字段。
    • 我把它记录在里面,if (cell == nil) 代码中的代码永远不会被调用,尝试了多个表,多次。现在的代码确实在其中创建了文本字段,并且每次我去表时,它都是一个空白调用。只有常规单元格会记录它的 nil,但我的自定义单元格不会,我也尝试过使用多种单元格类型,例如带有滑块的单元格或带有开关的单元格
    • @MaxHasADHD - 抱歉,这是一个剪切和粘贴错误。看看修改后的代码。
    • 我没有复制粘贴它,所以我已经这样了。我不知道为什么它不是零,这是现在的确切代码:pastie.org/5082256 并且是 textFieldCell 每次记录的内容:>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 2012-11-13
    • 2014-11-17
    • 1970-01-01
    相关资源
    最近更新 更多