简体   繁体   中英

UITableView is not refreshing when scrolling

I have UITableView, with CustomCell. In CustomCell there is UICollectionView with CustomUICollectionViewCell. Everything is created in the way, according to UICollectionView inside CustomTableCell . Then, let's say I have 12 rows. On the beginning I can see 6 of them. In CustomUICollectionViewCell, labels are fine, but when I scroll down, to see other cells, CustomUICollectionViewCells labels are not as expected. They are the same as first 6 rows, which are now scrolled up.

Where should I refresh CustomUICollectionViewCells label after scroll?

Example view: Before scrolling: 在此处输入图片说明

After scrolling: 在此处输入图片说明

However when debugging, when assigning data to cell, everything is assigned fine. All code is in the link as it's also my question. Seems that it's duplicating rows. I guess that it should be reloaded(?).

Added prepareForReuse method, as advised, but didn't help. Now my CustomCellClass looks:

@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

And my 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

My 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;
}

Because UITableViewCells are reused for performance, caching can sometimes occur. In your TemperatureTableViewCell class you should override prepareForReuse and reset its properties. In your case you should clear the data you are populating it with. I would also keep prepareForReuse in TemperatureItemCollectionViewCell to avoid anymore issues.

- (void)prepareForReuse
{
  [super prepareForReuse];

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

I think this is what's happening. Inside of your child cell (I think it's collectionViewCell) you should reset all the values of the ui elements. Basically you are reusing one cell and you neeed to reset the values of the text or the images on it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM