gpt4 book ai didi

ios - UITableViewCell 子类的内容重复

转载 作者:行者123 更新时间:2023-11-28 22:09:44 25 4
gpt4 key购买 nike

我有一个 UItableViewCell 子类,代码如下:

#import "RegisterTableViewCell.h"

@implementation RegisterTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code

_titolo = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 80, self.frame.size.height)];
_titolo.numberOfLines = 0;
_titolo.lineBreakMode = NSLineBreakByWordWrapping;
[self.contentView addSubview:_titolo];

_linearettangolare = [[UIView alloc]initWithFrame:CGRectMake(100, 10, 1, 25)];
_linearettangolare.backgroundColor = [UIColor whiteColor];
[self.contentView addSubview:_linearettangolare];

_input = [[UITextField alloc]initWithFrame:CGRectMake(_linearettangolare.frame.origin.x+6, 10, self.frame.size.width-110, 25)];

[self.contentView addSubview:_input];
}
return self;
}

- (void)awakeFromNib
{
// Initialization code
}



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

// Configure the view for the selected state
}

@end

和一个cellforrowatindexath 方法这样写:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *cellIdentifier = @"Cell";

RegisterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[RegisterTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

cell.titolo.text = @"Conferma password:";
cell.titolo.font = [UIFont boldSystemFontOfSize:13];

if (indexPath.row%2 == 0) {
cell.backgroundColor = [UIColor colorWithRed:143/255.0 green:177/255.0 blue:188/255.0 alpha:1.0/1.0];
cell.titolo.textColor = [UIColor whiteColor];
cell.linearettangolare.backgroundColor = [UIColor whiteColor];
cell.input.textColor = [UIColor whiteColor];
cell.input.secureTextEntry = FALSE;


}
else{
cell.backgroundColor = [UIColor colorWithRed:175/255.0 green:209/255.0 blue:227/255.0 alpha:1.0/1.0];
cell.titolo.textColor = [UIColor colorWithRed:138/255.0 green:140/255.0 blue:142/255.0 alpha:1.0/1.0];
cell.linearettangolare.backgroundColor = [UIColor colorWithRed:138/255.0 green:140/255.0 blue:142/255.0 alpha:1.0/1.0];
cell.input.textColor =[UIColor colorWithRed:138/255.0 green:140/255.0 blue:142/255.0 alpha:1.0/1.0];
cell.input.secureTextEntry = TRUE;
}

return cell;
}

我遇到了以下问题:

当我的 UITableView 上下滚动时,然后我将文本添加到 inputtextfield,该内容“迁移”到另一个 UITableViewCell

我的代码有什么问题?

最佳答案

我认为您的“问题”是单元格被重复使用,并且它们的内容在被重复使用之前从未被重置。

要么在使用prepareForReuse 重用单元格之前准备单元格(参见 here )
或清除 cellForRowAtIndexPath

中的输入字段

根据 Apple 指南,它(对于单元格内容)的首选方式是在 cellForRowAtIndexPath

For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell.

关于ios - UITableViewCell 子类的内容重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23198119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com