繁体   English   中英

UICollectionView reloadData仅刷新第一项

[英]UICollectionView reloadData refreshes only first item

当我执行[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:调用,是因为您的集合视图只能显示一个单元格。 如果滚动它,则应该看到另一个。

尝试使用这个:

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

    if(!deviceCell.label)
     {
       [deviceCell prepareForReuse];

        [deviceCell.contentView addSubview:deviceCell.label];
     }

        PeripheralTracking *peripheral=self.currentPeripherals[indexPath.item];
        [deviceCell configureLabelWithPeripheralTracking:peripheral forItem:indexPath.item];
   return deviceCell;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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