记一次UITableViewCell嵌套UICollectionViewCell切换数据时的展示问题


层次结构是UITableView里面有UITableViewCell,UITableViewCell里面有UICollectionView,UICollectionView里面有UICollectionViewCell,UICollectionViewCell里面包含各种控件。

问题是首次加载有个控件不展示,点击切换后没有问题

重要的一点:但是在用xcode查看层次结构是有的

首次加载时候的执行顺序是

1.collectionView cellForItemAtIndexPath 的 collectionView dequeueReusableCellWithReuseIdentifier

2.CreditCardCollectionViewCell  initWithFrame:(CGRect)frame

3.UICollectionViewCell里面包含的各种控件init alloc

4.collectionView cellForItemAtIndexPath 的 return cell

5 UICollectionViewCell 的 layoutSubviews

可见第一次加载是在return cell之后才走的layoutSubviews方法,尝试的解决办法

1.首次给这个控件一个固定坐标,不管用

2.是不是应该在return cell之前走layoutSubviews?点击切换时,重新创建这个UICollectionViewCell,在return cell之前都做好,不管用

3.在return之前强制调用layoutSubviews。不管用

4.在即将展示cell的时候,调用layoutSubviews

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [cell layoutSubviews];
    });
}

问题解决。



猜你喜欢

转载自blog.csdn.net/qq_15509071/article/details/79861802