如何根据单元格的内容在 UICollectionView 中准备 prepareForSegue?
How to prepareForSegue in a UICollectionView depending on the contents of cell?
我有一个带有自定义单元格的 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)上显示我需要的内容。我现在正在苦苦挣扎,因为有一个依赖于单元格 selected 的 segue。我已成功地为 UIImageView NSArray 执行此操作 - 但我不熟悉如何使用 UICollectionView 和 UICollectionViewCell 解决此问题。
我想执行 segue 以进一步显示(取决于哪个单元格被 selected),字典中尚未使用的一些值(即描述)。
我很困惑,理想的方法是写在 performSegue 方法中还是在 didSelectItemAtIndexPath 中?
下面是一些额外的代码和屏幕截图以获取更多信息;
带有 segue 标识符的两个视图控制器的故事板
初始viewcontroller与UICollectionView有关如何填充数组和UICollectionViewlabels/images的代码
- (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
- 我得到不同的值取决于我 select 的单元格。所以我假设辅助 VC 正在根据单元格 selected 加载不同的实例?然而,我更困惑的是为什么在分配硬字符串值时实际属性甚至不会改变?
我假设您的实际问题是 理想的方法是写在 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
的 indexPathForCell
方法来实现,如 this answer.
中更详细的描述
请注意,如果您使用专用模型 class(我之前提到的 Event
模型)然后将一组事件作为数据源,它可能会大大简化您的代码。
我有一个带有自定义单元格的 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)上显示我需要的内容。我现在正在苦苦挣扎,因为有一个依赖于单元格 selected 的 segue。我已成功地为 UIImageView NSArray 执行此操作 - 但我不熟悉如何使用 UICollectionView 和 UICollectionViewCell 解决此问题。
我想执行 segue 以进一步显示(取决于哪个单元格被 selected),字典中尚未使用的一些值(即描述)。
我很困惑,理想的方法是写在 performSegue 方法中还是在 didSelectItemAtIndexPath 中?
下面是一些额外的代码和屏幕截图以获取更多信息;
带有 segue 标识符的两个视图控制器的故事板
初始viewcontroller与UICollectionView有关如何填充数组和UICollectionViewlabels/images的代码
- (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
- 我得到不同的值取决于我 select 的单元格。所以我假设辅助 VC 正在根据单元格 selected 加载不同的实例?然而,我更困惑的是为什么在分配硬字符串值时实际属性甚至不会改变?
我假设您的实际问题是 理想的方法是写在 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
的 indexPathForCell
方法来实现,如 this answer.
请注意,如果您使用专用模型 class(我之前提到的 Event
模型)然后将一组事件作为数据源,它可能会大大简化您的代码。