Ios UICollectionView重新加载数据仅刷新第一项
当我执行Ios UICollectionView重新加载数据仅刷新第一项,ios,objective-c,uicollectionview,reload,nsnotificationcenter,Ios,Objective C,Uicollectionview,Reload,Nsnotificationcenter,当我执行[self.collectionView reloadData]时,仅重新加载我的第一项 即使-(NSInteger)collectionView:(UICollectionView*)view numberOfItemsInSection:(NSInteger)section并存储两个项,然后对第一个项只调用一次(UICollectionViewCell*)collectionView:(UICollectionView*)cv cellForItemAtIndexPath:(NSIn
[self.collectionView reloadData]时代码>,仅重新加载我的第一项
即使-(NSInteger)collectionView:(UICollectionView*)view numberOfItemsInSection:(NSInteger)section例如,调用code>并存储两个项,然后对第一个项只调用一次(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有几个bug,但是前面描述的解决方案都不适用于我的案例。我已经尝试过使用reloadeitemsatindexpaths
和reloadesections
,但这不起作用,有时会出现异常
我也厌倦了按程序添加项目,但没有成功:
[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;
}
尝试使用以下方法:
- (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;
}
将每个单元格的高度设置为150:
[aFlowLayout setItemSize:CGSizeMake(400, 150)]
尝试将其更改为45:
[aFlowLayout setItemSize:CGSizeMake(400, 45)]
或者,如果单元格大小因单元格而异,则可以实现collectionView:layout:sizeFormiteIndeXPath:
。您只看到对collectionView:cellForItemAtIndexPath:
的一个调用的原因是,您的集合视图只能显示一个单元格。如果您滚动它,您应该会看到另一个。您的排序正确吗?是的,我在对外围设备进行排序时获得了正确的NSArray-这不是问题。您设置文本的位置。.将其置于cellForItemAtIndexPathoutside If条件的循环之外。.您可以设置我很抱歉,但这不是问题所在