【问题标题】:Custom UITableViewCell XIB repeats row after scrolling自定义 UITableViewCell XIB 滚动后重复行
【发布时间】:2015-02-07 09:42:07
【问题描述】:

我有一个带有 xib 的自定义 UITableViewCell。

产品单元

还有另一个带有 Tableview 的视图控制器。

现在我想将 Cell 添加到 tableview 中。

这是我的代码。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.tblList.dataSource = self;

    [self.tblList registerNib:[UINib nibWithNibName:@"ProductCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MyIdentifierList"];

//other code lines apart from tableview.
}

TableView 数据源

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.aryData count];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100.0;
}

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

    ProductCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifierList"];

    NSDictionary *dict = [self.aryData objectAtIndex:indexPath.row];
    cell.lblProductName.text = [dict objectForKey:@"Product"];

    cell.tag = indexPath.row;

    NSLog(@"CELL : %@",cell);

    return cell;
}

您将看到该实例重复的控制台数据。 (它会导致单元格的复选框和单元格的 UIstepper 值出现问题,它们是单元格的子视图)。

2014-12-09 18:45:43.619 RetailCheckOut[4777:132965] CELL : <ProductCell: 0x7f9d9b804b90; baseClass = UITableViewCell; frame = (0 0; 320 80); autoresize = RM+BM; layer = <CALayer: 0x7f9d994b6330>>
2014-12-09 18:45:43.622 RetailCheckOut[4777:132965] CELL : <ProductCell: 0x7f9d99663780; baseClass = UITableViewCell; frame = (0 0; 320 80); autoresize = RM+BM; tag = 1; layer = <CALayer: 0x7f9d99664b90>>
2014-12-09 18:45:43.625 RetailCheckOut[4777:132965] CELL : <ProductCell: 0x7f9d996a5870; baseClass = UITableViewCell; frame = (0 0; 320 80); autoresize = RM+BM; tag = 2; layer = <CALayer: 0x7f9d996a56b0>>
2014-12-09 18:45:43.628 RetailCheckOut[4777:132965] CELL : <ProductCell: 0x7f9d9b817e50; baseClass = UITableViewCell; frame = (0 0; 320 80); autoresize = RM+BM; tag = 3; layer = <CALayer: 0x7f9d9b817c90>>
2014-12-09 18:45:43.630 RetailCheckOut[4777:132965] CELL : <ProductCell: 0x7f9d9b81e810; baseClass = UITableViewCell; frame = (0 0; 320 80); autoresize = RM+BM; tag = 4; layer = <CALayer: 0x7f9d9b81e650>>
2014-12-09 18:45:43.633 RetailCheckOut[4777:132965] CELL : <ProductCell: 0x7f9d9b825140; baseClass = UITableViewCell; frame = (0 0; 320 80); autoresize = RM+BM; tag = 5; layer = <CALayer: 0x7f9d9b824fd0>>
2014-12-09 18:45:54.978 RetailCheckOut[4777:132965] CELL : <ProductCell: 0x7f9d996a7400; baseClass = UITableViewCell; frame = (0 0; 320 80); autoresize = RM+BM; tag = 6; layer = <CALayer: 0x7f9d996b4520>>
2014-12-09 18:45:55.241 RetailCheckOut[4777:132965] CELL : <ProductCell: 0x7f9d9b804b90; baseClass = UITableViewCell; frame = (0 0; 375 100); hidden = YES; autoresize = W; tag = 7; layer = <CALayer: 0x7f9d994b6330>>
2014-12-09 18:45:56.824 RetailCheckOut[4777:132965] CELL : <ProductCell: 0x7f9d99663780; baseClass = UITableViewCell; frame = (0 100; 375 100); hidden = YES; autoresize = W; tag = 8; layer = <CALayer: 0x7f9d99664b90>>
2014-12-09 18:45:57.045 RetailCheckOut[4777:132965] CELL : <ProductCell: 0x7f9d996a5870; baseClass = UITableViewCell; frame = (0 200; 375 100); hidden = YES; autoresize = W; tag = 9; layer = <CALayer: 0x7f9d996a56b0>>

ProductCell 类 xib 不在 Storyboard 中,而是在单独的。

【问题讨论】:

    标签: iphone uitableview storyboard objective-c-blocks


    【解决方案1】:

    这是正常行为。 UITableView 出于性能原因重复使用单元格。您实际上已经告诉它自己执行此操作,在这里:

        ProductCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifierList"];
    

    注意dequeueReusableCellWithIdentifier: 中的dequeueReusable - 此方法会尽可能重用 tableView 队列中的单元格。

    我无法从您发布的代码中看到您的步进器和复选框发生了什么,但您可能需要做的是在 tableView:cellForRowAtIndexPath: 中设置它们的值。

    【讨论】:

    • 另外,在 ProductCell 的 .m 文件中,您可以实现 prepareForReuse ,您可以在其中将单元格返回到其默认状态。
    • 这里重复单元格的意思是,我有 50 行,首先它显示 7 行,一旦我开始滚动,我就会返回相同的实例。因此无法正确设置值。查看此实例:0x7f9d9b804b90 在 7 行后重复。
    • 是的,这就是应该发生的事情。为什么这意味着您无法为 Stepper 和 Checkbox 设置正确的值?你有他们的出路吗?
    • 是的,我在 ProductCell 插座中添加了 UIStepper 和 UIButton(自定义复选框)。
    • 好的,所以你应该在tableView:cellForRowAtIndexPath:中为它们设置正确的值/状态
    猜你喜欢
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多