gpt4 book ai didi

ios - 停止在 UITableViewCell 中重复值

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

我已经使用自定义 UITableViewCell 类以编程方式创建了一个 UITableViewCell,但是由于某些原因,即使从 NSMutableArray 读取的值是正确的,并且我正在 prepareForReuse 的自定义 UITableViewCell 类。

这就是 cellForRow:AtIndexPath 方法的样子

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cellDictionary = [xmlMArray objectAtIndex:indexPath.row];
static NSString *CellIdentifier = @"Cell";

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MatchingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.itemsDictionary = cellDictionary;
cellDictionary = nil;
[cell drawCell];

return cell;
}

然后在里面,这就是我的自定义类的样子

#import "MatchingCell.h"
#import "ScreenSize.h"

@implementation MatchingCell

@synthesize itemsDictionary;
@synthesize nameString;
@synthesize addressString;
@synthesize codeString;
@synthesize scrollCell;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
nameString = [[UILabel alloc] init];
nameString.backgroundColor = [UIColor clearColor];

addressString = [[UILabel alloc] init];
addressString.backgroundColor = [UIColor clearColor];

codeString = [[UILabel alloc] init];
codeString.backgroundColor = [UIColor clearColor];

scrollCell = [[UIScrollView alloc] init];
scrollCell.backgroundColor = [UIColor whiteColor];

[scrollCell addSubview:nameString];
[scrollCell addSubview:addressString];
[scrollCell addSubview:codeString];
[self addSubview:scrollCell];
}
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
}

- (void)drawCell
{
nameString.frame = CGRectMake(15.0, 0.5, 70.0, 40.0);
nameString.text = [itemsDictionary objectForKey:@"Name"];

addressString.frame = CGRectMake(105.0, 0.5, 95.0, 40.0);
addressString.text = [itemsDictionary objectForKey:@"Address"];

codeString.frame = CGRectMake(220.0, 10.5, codeString.frame.size.width, 50.0);
codeString.text = [NSString stringWithFormat:@"ISN %@: %@",[itemsDictionary objectForKey:@"CodeA"] ,[itemsDictionary objectForKey:@"CodeB"]];
[codeString sizeToFit];

scrollCell.frame = CGRectMake(0.0, 0.0, ScreenWidth, 45.0);
[scrollCell setContentSize:(CGSizeMake((220.0 + codeString.frame.size.width)+15, 45.0))];


}

- (void)prepareForReuse
{
[super prepareForReuse];

nameString = nil;
nameString.text = nil;
addressString = nil;
addressString.text = nil;
codeString = nil;
codeString.text = nil;

itemsDictionary = nil;

[self didTransitionToState:UITableViewCellStateDefaultMask];
}

@end

所以我的问题是,当您将新的 UITableViewCells 滚动到 View 中时,如何停止重复值?

谢谢。

最佳答案

将init中的所有内容都取出来,并将其移动到drawCell中:

您清除了所有引用以准备重用,但因此它们永远不会被重新创建:

if (cell == nil) {
cell = [[MatchingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

所以你的 init 只在第一次运行。

单元格仍然存在,但它对其 subview 的引用都不存在。

两个注意事项:

在清零引用之前,您还应该从 subview 中移除以准备重用。因为否则你会继续在 subview 之上添加 subview ,你会得到糟糕的性能。 (如@user2891327 所建议)

您不应该将 View 直接添加到单元格,而是将其 .contentView 属性用于 subview 。

关于ios - 停止在 UITableViewCell 中重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22367379/

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