【发布时间】:2017-07-24 17:07:51
【问题描述】:
我有一个带有自定义单元格的 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 在一个数组标签库中都有与之关联的字符串。
我已经能够让 UICollectionView 在特定视图控制器(eventTitle、eventLocation、eventPrice、eventIcon)上显示我需要的内容。我现在正在苦苦挣扎,因为它有一个依赖于所选单元格的转场。我已经成功地为 UIImageView NSArray 执行此操作 - 但我不熟悉如何使用 UICollectionView 和 UICollectionViewCell 解决此问题。
我想执行转场以进一步显示(取决于选择了哪个单元格),字典中的一些尚未使用的值(即描述)。
我很困惑,理想的方法是写在 performSegue 方法中还是写在 didSelectItemAtIndexPath 中??
下面是一些额外的代码和屏幕截图以获取更多信息;
两个带有 segue 标识符的视图控制器的故事板
带有 UICollectionView 的初始 viewcontroller 代码说明如何填充数组以及 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";
}
}
}
辅助视图控制器甚至不会将其属性collectionView.eventTitle.text 更新为@"Hello" 的硬字符串。虽然当我NSLog NSIndexPath *index - 我得到不同的值取决于我选择的单元格。所以我假设辅助 VC 正在加载依赖于所选单元的不同实例?然而,我更困惑的是为什么在分配硬字符串值时实际属性甚至不会改变?
【问题讨论】:
标签: objective-c uicollectionview segue uicollectionviewcell