UICollectionView reloadData仅刷新第一个项目

时间:2014-03-07 13:17:04

标签: 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;
}

2 个答案:

答案 0 :(得分:1)

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

[aFlowLayout setItemSize:CGSizeMake(400, 150)]

尝试将其更改为45:

[aFlowLayout setItemSize:CGSizeMake(400, 45)]

如果您的小区大小因小区而异,您也可以实施collectionView:layout:sizeForItemAtIndexPath:。您只看到对collectionView:cellForItemAtIndexPath:的一次调用的原因是您的集合视图只有显示一个单元格的空间。如果你滚动它,你应该看到另一个。

答案 1 :(得分:0)

尝试使用:

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