当我执行[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 有几个错误,但前面描述的解决方案都不适合我的情况。我已经尝试过使用 reloadItemsAtIndexPaths
和 reloadSections
但这没有用,有时会出现异常。
我也厌倦了以编程方式添加项目但没有成功:
[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/