ios - UICollectionView reloadData 仅刷新第一项

标签 ios objective-c uicollectionview reload nsnotificationcenter

当我执行[self.collectionView reloadData];时,仅重新加载我的第一个项目。

即使 - (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section; 被调用并存储,例如,两个项目,然后 (UICollectionViewCell *)collectionView :(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath 仅对第一个项目调用一次。

我已经验证在 numberOfItemsInSection 中,边界是框架的大小足以存储至少三个我的项目。

numberOfItemsInSection:2 frame: {{0, 0}, {400, 150}} and bounds {{0, 0}, {400, 150}}

每个 UICollectionViewCell 都是 {0, 0}、{400,45},并且按照 init 方法中的指定垂直存储。

我读到 UICollectionView 和 ios7 有几个错误,但前面描述的解决方案都不适合我的情况。我已经尝试过使用 reloadItemsAtIndexPathsreloadSections 但这没有用,有时会出现异常。

我也厌倦了以编程方式添加项目但没有成功:

[self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:   (self.currentPeripherals.count -1) inSection:0]]];

这就是我的 UICollectionViewController 的样子:

- (instancetype)init
{
    UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    [aFlowLayout setItemSize:CGSizeMake(400, 150)];
    [aFlowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    self = [self initWithCollectionViewLayout:aFlowLayout];
    if (self) {
        self.view.frame=CGRectMake(0,0,400,150);
        self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        self.collectionView.backgroundView=[[UIView alloc] initWithFrame:CGRectZero];;
        self.view.layer.backgroundColor=[UIColor clearColor].CGColor;
        self.collectionView.backgroundColor=[UIColor clearColor];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
    }
    return self;
}

- (void)sharedInit {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleBTPOSHostDidUpdatePeripheralsNotification:) name:BTPOSHostDidUpdatePeripheralsNotification object:nil];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

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

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DeviceCell *deviceCell = [cv dequeueReusableCellWithReuseIdentifier:CellID forIndexPath:indexPath];

    if(!deviceCell.label)
    {
        [deviceCell prepareForReuse];
        PeripheralTracking *peripheral=self.currentPeripherals[indexPath.item];
        [deviceCell configureLabelWithPeripheralTracking:peripheral forItem:indexPath.item];
        [deviceCell.contentView addSubview:deviceCell.label];
    }

    return deviceCell;
}

- (void)handleBTPOSHostDidUpdatePeripheralsNotification:(NSNotification *)notification
{
    NSUInteger oldCount = self.currentPeripherals.count;

    NSDictionary *trackedPeripherals = notification.userInfo[BTPOSHostDidUpdatePeripheralsNotificationPeripheralsKey];
    [self sortPeripheralsDictionary:trackedPeripherals];
    [self.collectionView reloadData];
}

这就是我的外围设备的排序方式:

-(void)sortPeripheralsDictionary:(NSDictionary*)peripheralsDictionary
{
    NSMutableArray *dictValues = [[peripheralsDictionary allValues] mutableCopy];

    if(dictValues.count>1)
    {
        [dictValues sortUsingComparator: (NSComparator)^(PeripheralTracking *a, PeripheralTracking *b)
         {
             NSNumber *RSSI1 = 0;
             NSNumber *RSSI2 = 0;
             NSComparisonResult result = 0;

             if(a.averageRSSI)
             {
                 PeripheralTrackingRSSI doubleRSSIa = a.averageRSSI;
                 RSSI1 = [NSNumber numberWithDouble:doubleRSSIa];
             }
             if(b.averageRSSI)
             {
                 PeripheralTrackingRSSI doubleRSSIb = b.averageRSSI;
                 RSSI2 = [NSNumber numberWithDouble:doubleRSSIb];
             }

             if(RSSI1 && RSSI2)
             {
                 result = [RSSI2 compare:RSSI1];
             }

             return result;
         }
         ];
    }

    self.currentPeripherals = dictValues;
}

最佳答案

您将每个单元格的高度设置为 150:

[aFlowLayout setItemSize:CGSizeMake(400, 150)]

尝试将其更改为 45:

[aFlowLayout setItemSize:CGSizeMake(400, 45)]

或者您可以实现 collectionView:layout:sizeForItemAtIndexPath: 如果您的单元格大小因单元格而异。您只看到一次调用 collectionView:cellForItemAtIndexPath: 的原因是您的 Collection View 只有显示一个单元格的空间。如果滚动它,您应该会看到另一个。

关于ios - UICollectionView reloadData 仅刷新第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22251143/

相关文章:

ios - MMdrawercontroller:打开左侧菜单时点击centerVC

ios - 为什么 UICollectionViewDiffableDataSource 在没有任何变化的情况下重新加载每个单元格?

ios - "Command/usr/sbin/chown failed with exit code 1"归档时

iphone - 谁能告诉错误以下是什么以及如何解决这个问题(文件是通用的(3 片)但不包含 armv7s 片)?

ios - 从数组中选择一个随机按钮

ios - 在 didReceiveRemoteNotification 中加载特定 Controller

ios - 找不到 downloadimageurl 来下载 Collection View 单元格图像的图像

ios - 设置 contentInset 后 UICollectionView 不滚动

iphone - AFNetworking:返回 200 且无内容时运行失败 block

android - 我如何从 cordova 中的服务器获取推送通知?