xcode 自定义表格单元格 iOS 6

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12965535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:52:09  来源:igfitidea点击:

Custom table cells iOS 6

objective-cxcodeuitableviewios6

提问by Maximilian Litteral

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

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

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

和继承人的代码 - (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,

这是 UITableViewCell 子类中的代码,

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

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

回答by Martin R

See the documentation of dequeueReusableCellWithIdentifier:for iOS 6:

请参阅dequeueReusableCellWithIdentifier: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.

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

You have registered a class with

你已经注册了一个班级

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

and therefore dequeueReusableCellWithIdentifier:never returns nil.

因此dequeueReusableCellWithIdentifier:永远不会返回nil

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

您应该在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;
}

An alternative is to implement prepareForReusein your TextFieldTableCellclass to "clean up" the cell content for reuse.

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

回答by sosborn

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