ios - 停止在 UITableViewCell 中重复值

标签 ios iphone objective-c uitableview

我已经使用自定义 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/

相关文章:

ios - RNImageToBase64 native 模块因 URL 而崩溃

ios - Swift:从另一个 View Controller 调用函数使对象为零

html - 检查 NSString 是否是 html 字符串?

ios - 以编程方式关闭 iphone 低电量警报

iphone - 在objective-C中同步sqlite数据库

ios - 将 UIImage 绘制为一个没有离屏渲染的圆圈

ios - 枚举字符串而不是 int

iphone - iOS从NSString中删除前N个单词

iphone - UIImagePickerController didFinishPickingMediaWithInfo 未被调用

objective-c - 我们可以将类方法(以+号开头)与该类的实例一起使用吗