我有一个带有自定义单元格的UICollectionView,它显示字典的某些元素。
这是字典;
-(instancetype)initWithIndex:(NSUInteger)index{
self = [super init];
if (self) {
EventsLibrary *eventsLibrary = [[EventsLibrary alloc]init];
NSArray *library = eventsLibrary.library;
NSDictionary *eventsDictionary = library[index];
_eventTitle = [eventsDictionary objectForKey:kTitle];
_eventLocation = [eventsDictionary objectForKey:kLocation];
_eventPrice = [eventsDictionary objectForKey:kPrice];
_eventDate = [eventsDictionary objectForKey:kDate];
_eventTime = [eventsDictionary objectForKey:kTime];
_eventDescription = [eventsDictionary objectForKey:kDescription];
_eventIcon = [UIImage imageNamed:[eventsDictionary objectForKey:kIcon]];
_eventIconLarge = [UIImage imageNamed:[eventsDictionary objectForKey:kLargeIcon]];
_eventType = [eventsDictionary objectForKey:kType];
}
return self;
}
kVariables在标有库的数组中都有与之关联的字符串。
我已经能够在特定的视图控制器(eventTitle,eventLocation,eventPrice,eventIcon)上显示我需要的UICollectionView。我现在正在努力,因为有一个segue根据所选择的细胞作出反应。我已经成功地为UIImageView NSArray做了这个 - 但我对如何使用UICollectionView和UICollectionViewCell来解决这个问题并不熟悉。
我想执行一个segue,以便进一步显示(取决于选择了哪个单元格),字典中尚未使用的某些值(即描述)。
我是否会在performSegue方法或didSelectItemAtIndexPath中写出理想方法?
以下是一些额外的代码和屏幕截图,以获取更多信息;
具有segue标识符的两个视图控制器的故事板
初始viewcontroller的代码,其中包含关于如何填充数组以及UICollectionView标签/图像关联的UICollectionView
- (void)viewDidLoad {
[super viewDidLoad];
self.eventTitleArray = [[NSMutableArray alloc]initWithCapacity:8];
self.eventLocationArray = [[NSMutableArray alloc]initWithCapacity:8];
self.eventIconArray = [[NSMutableArray alloc]init];
self.eventPriceArray = [[NSMutableArray alloc]initWithCapacity:8];
self.eventTypeArray = [[NSMutableArray alloc]initWithCapacity:8];
self.eventDateArray = [[NSMutableArray alloc]initWithCapacity:10];
for (NSUInteger index = 0; (index < 8) ; index++){
EventsList *eventList = [[EventsList alloc] initWithIndex:index];
NSString *individualEventTitle = eventList.eventTitle;
NSString *individualEventLocation = eventList.eventLocation;
NSString *individualEventIcon = eventList.eventIcon;
NSString *individualEventPrice = eventList.eventPrice;
NSString *individualEventType = eventList.eventType;
NSArray *eventDate = eventList.eventDate;
[self.eventTitleArray addObject:individualEventTitle];
[self.eventLocationArray addObject:individualEventLocation];
[self.eventIconArray addObject:individualEventIcon];
[self.eventPriceArray addObject:individualEventPrice];
[self.eventTypeArray addObject:individualEventType];
[self.eventDateArray addObjectsFromArray:eventDate];
}
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
EventsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"eventsCell" forIndexPath:indexPath];
cell.eventImage.image = [self.eventIconArray objectAtIndex:indexPath.row];
cell.eventTitle.text = [self.eventTitleArray objectAtIndex:indexPath.row];
cell.eventLocation.text = [self.eventLocationArray objectAtIndex:indexPath.row];
cell.eventPrice.text = [self.eventPriceArray objectAtIndex:indexPath.row];
cell.eventType.text = [self.eventTypeArray objectAtIndex:indexPath.row];
return cell;
}
我当前能够点击模拟器中的单元格,但第二个视图控制器没有内容 - 由于segue中没有方法而推测
因此,我很欣赏一些关于如何使用UICollectionView解决这个问题的知识?
prepareForSegue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
EventView *collectionView = [[EventView alloc]init];
if([segue.identifier isEqual: @"showEventDetail"]){
if([self.yourEvents indexPathForCell:sender]){
NSIndexPath *index = [self.yourEvents indexPathForCell:sender];
collectionView.eventTitle.text = @"Hello";
collectionView.eventDescription.text = @"This Event";
}
}
}
辅助viewController甚至不会将其属性collectionView.eventTitle.text
更新为@"Hello"
的硬字符串。虽然当我NSLog
NSIndexPath *index
时 - 我会根据我选择的单元格得到不同的值。所以我假设辅助VC根据所选的单元加载不同的实例?然而,我更为困惑的是,为什么在分配硬字符串值时,实际属性甚至不会改变?
答案 0 :(得分:0)
我假设您的实际问题是是否将在performSegue方法或didSelectItemAtIndexPath 中编写理想方法。
如果是这种情况,两者都可以发挥作用。
您可以先尝试didSelectItemAtIndexPath
并将模型对象(在您的情况下可能是Event
)传递给详细视图控制器。这看起来像这样:
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
// Get the Event model representing the currently selected cell
Event *selectedEvent = self.events[indexPath.row];
// Create a new detail view controller with the selected event and push it
EventDetailViewController *eventDetailViewController = [[EventDetailViewController alloc] initWithEvent: selectedEvent];
[self.navigationController pushViewController:eventDetailViewController animated:YES];
}
如果您想使用segues,您的目标是再次获得所选的indexPath
。您可以使用UICollectionView
的{{1}}方法来实现这一目标,详见this answer中所述。
请注意,如果您使用专用模型类(我前面提到的indexPathForCell
),然后将一组事件作为您的数据源,它可能会大大简化您的代码。