【发布时间】:2014-04-10 15:53:57
【问题描述】:
当我执行[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;
}
【问题讨论】:
-
你的排序正确吗?
-
是的,我在对外围设备进行排序时获得了正确的 NSArray - 这不是问题。
-
你设置文本的地方..把它放在cellForItemAtIndexPath中的循环之外
-
if条件外..可以设置
标签: ios objective-c uicollectionview reload nsnotificationcenter