gpt4 book ai didi

ios - 为什么滚动 UICollectionView 时我的单元格 subview 框架会发生变化

转载 作者:行者123 更新时间:2023-11-29 03:03:30 29 4
gpt4 key购买 nike

基本上,我有一个从数据源加载值的 UICollectionViewCell,此外它应该将 subview 框架的高度设置为等于数据源中的值之一。

我遇到的问题是,当我第一次运行项目时它看起来是正确的,但是当我来回滚动几次时,由于单元格被重用,文本值保持不变但 subview 的框架发生了变化.

让我感到困惑的是,在单元格中设置标签文本的变量与在同一单元格中设置 subview 高度值的变量相同;但是标签文本总是正确的,但是 UIView 框架的高度会随着滚动而不断变化。

我知道这可能与细胞的重复使用方式有关,但我无法确定。

下面是我的 cellForRowAtIndexPath 代码。谢谢!

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{

DailyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DayCell" forIndexPath:indexPath];

cell.backgroundContainerView.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.3f];

float total = [[dummyData objectAtIndex:indexPath.row][@"total"] floatValue] * 2;

UIView * backgroundFillView = [UIView new];
if (![cell viewWithTag:1000]) {
NSLog(@"Creating backgroundFillView on Cell: %ld", (long)indexPath.row);

backgroundFillView.tag = 1000;
[cell.backgroundContainerView addSubview:backgroundFillView];

}


cell.debugCellNumber.text = [NSString stringWithFormat:@"%ld", (long)indexPath.row];
cell.debugCellTotal.text = [NSString stringWithFormat:@"%f", total];

backgroundFillView.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.5f];
backgroundFillView.frame = CGRectMake(0, 200 - total, 60, total);

NSLog(@"Cell: %ld, total: %f", (long)indexPath.row, total);
NSLog(@"Cell: %ld, backgroundFillView Height: %f", indexPath.row, backgroundFillView.frame.size.height);
NSLog(@"Cell: %ld, backgroundFillView Y: %f", indexPath.row, backgroundFillView.frame.origin.y);

return cell;
}

最佳答案

您只需在第一次填充单元格时添加一个backgroundFillView。重新使用时不会。

替换:

UIView * backgroundFillView = [UIView new];
if (![cell viewWithTag:1000]) {
NSLog(@"Creating backgroundFillView on Cell: %ld", (long)indexPath.row);

backgroundFillView.tag = 1000;
[cell.backgroundContainerView addSubview:backgroundFillView];

}

与:

UIView * backgroundFillView = [cell viewWithTag:1000];
if (! backgroundFillView) {
NSLog(@"Creating backgroundFillView on Cell: %ld", (long)indexPath.row);
backgroundFillView = [UIView new];

backgroundFillView.tag = 1000;
[cell.backgroundContainerView addSubview:backgroundFillView];

}

关于ios - 为什么滚动 UICollectionView 时我的单元格 subview 框架会发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23031064/

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