簡體   English   中英

滾動時,UITableView不會刷新

[英]UITableView is not refreshing when scrolling

我有UITableView和CustomCell。 在CustomCell中,有帶有CustomUICollectionViewCell的UICollectionView。 根據CustomTableCell中的UICollectionView ,所有內容都是以這種方式創建的。 然后,假設我有12行。 一開始我可以看到其中的6個。 在CustomUICollectionViewCell中,標簽很好,但是當我向下滾動以查看其他單元格時,CustomUICollectionViewCells標簽與預期不符。 它們與前6行相同,現在已向上滾動。

滾動后應該在哪里刷新CustomUICollectionViewCells標簽?

示例視圖:滾動之前: 在此處輸入圖片說明

滾動后: 在此處輸入圖片說明

但是,在調試時,將數據分配給單元時,一切都分配良好。 所有代碼都在鏈接中,因為這也是我的問題。 似乎是在重復行。 我猜應該重新加載(?)。

根據建議添加了prepareForReuse方法,但沒有幫助。 現在我的CustomCellClass看起來:

@implementation TemperatureTableViewCell

    NSNumberFormatter *decimalStyleFormatter;

- (void)awakeFromNib
{
    [super awakeFromNib];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

    [flowLayout setItemSize:CGSizeMake(50, 50)];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    self.collectionView = [[CollectionView alloc] initWithFrame: CGRectZero collectionViewLayout:flowLayout];
    [self.collectionView registerClass:[TemperatureItemCollectionViewCell class] forCellWithReuseIdentifier:@"TemperatureItemCollectionCell"];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;

    [self.contentView addSubview:self.collectionView];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.collection.count;
}

-(void)collectionView:(CollectionView *)collectionView willDisplayCell:(TemperatureItemCollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    TemperatureSensor *temp = self.collection[indexPath.item];
    cell.temperatureValue = temp.temperature.stringValue;
    cell.tempValTempCollViewCell.text = [NSString stringWithFormat:@"%@°C", cell.temperatureValue];
    cell.sensorName.text = temp.sensorName;
    cell.imageTempCollViewCell.image = [TemperatureImageHelper getTemperatureIcon:temp];
     [self.collectionView reloadData];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TemperatureItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TemperatureItemCollectionCell" forIndexPath:indexPath];
    return cell;
}

- (void)setCollectionViewDelegate:(id)dataSourceDelegate indexPath:(NSIndexPath *)indexPath {
    self.collectionView.delegate = dataSourceDelegate;
}

@end

還有我的CustomCollectionViewCell:

@implementation TemperatureItemCollectionViewCell
-(void)awakeFromNib {
    [super awakeFromNib];

}
-(void)prepareForReuse {
    [super prepareForReuse];

    self.imageTempCollViewCell.image = nil;
    self.tempValTempCollViewCell.text = nil;
    self.sensorName.text = nil;
    self.temperatureValue = nil;

}
@end

我的TableViewController

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_tempSensorsDictionary == nil)
        return 0;
    else {
        NSArray *allSensorKeys = [_tempSensorsDictionary allKeys];
        return [allSensorKeys count];
    }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(TemperatureTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell setCollectionViewDelegate:self indexPath:indexPath];    
}


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

    NSArray *nodeNumbers = [_tempSensorsDictionary allKeys];
    TemperatureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath: indexPath];
    cell.collection = [_tempSensorsDictionary  objectForKey:nodeNumbers[indexPath.row]];
    //NSArray *nodeNumbers = [_tempSensorsDictionary allKeys];
    if([[UnitNameCache sharedUnitNameCache] getNameForId:nodeNumbers[indexPath.row]] != nil &&
       ![[[UnitNameCache sharedUnitNameCache] getNameForId:nodeNumbers[indexPath.row]] isEqualToString:@""]) {
        NSString *name = [NSString stringWithFormat:@"%@ (%@)", [[UnitNameCache sharedUnitNameCache] getNameForId:nodeNumbers[indexPath.row]], nodeNumbers[indexPath.row]];
        cell.unitNameLabel.text = name;
    } else {
        cell.unitNameLabel.text = nodeNumbers[indexPath.row];
    }
    return cell;
}

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

因為UITableViewCells被重新使用以提高性能,所以有時可能會發生緩存。 TemperatureTableViewCell類中,您應該覆蓋prepareForReuse並重置其屬性。 在您的情況下,您應該清除填充數據的數據。 我還將在TemperatureItemCollectionViewCell保持prepareForReuse以避免任何問題。

- (void)prepareForReuse
{
  [super prepareForReuse];

  //Reset properties here.
  self.collection = nil;
  [self.collectionView reloadData];
}

我認為這是正在發生的事情。 在子單元格(我認為是collectionViewCell)中,您應該重置ui元素的所有值。 基本上,您正在重用一個單元格,並且需要重置文本或圖像上的值。

override func prepareForReuse() {
    super.prepareForReuse()
    titleLabel1.text = nil
    image1.image = nil
    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM