【发布时间】:2015-10-22 21:49:53
【问题描述】:
我有一个 UICollectionView,其中填充了来自 CoreData 的内容。当视图加载时,我想预选/突出显示一些单元格。
我已经能够使用下面的代码获得预先选择的项目。突出显示并没有像我想要的那样无缝地工作,但我可以让它工作。
- (void)preSelect {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:6 inSection:5];
[self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
if ([[self.collectionView cellForItemAtIndexPath:indexPath] isSelected]) {
[self.collectionView cellForItemAtIndexPath:indexPath].backgroundColor = [UIColor darkGrayColor];
NSLog(@"selected count %i",[self.collectionView indexPathsForSelectedItems].count);
}
}
我不想让预选硬编码。我想做类似的事情:
NSIndexPath = indexPath where [value of cell] isEqual:@"NSString";
或者更好
PFQuery *query = [PFQuery queryWithClassName:@"myClass"];
[query findObjectsInBackgroundWithBlock:^(NSArray *object, NSError *error){ NSString *abc = [object valueForKey:@"myKey"];
NSIndexPath = indexPath where cell.text = abc;
【问题讨论】:
-
不是最优雅的答案,但我基本上可以通过一长串 if 语句来检查每个语句的值。我想要一个更好的答案:
标签: ios core-data uicollectionview nsindexpath