【问题标题】:How to prepareForSegue in a UICollectionView depending on the contents of cell?如何根据单元格的内容在 UICollectionView 中准备 ForSegue?
【发布时间】: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


    【解决方案1】:

    我假设您的实际问题是理想的方法是写在 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。您可以使用UICollectionViewindexPathForCell 方法来实现这一点,如this answer 中更详细描述的那样。

    请注意,如果您使用专用模型类(我之前提到的Event)然后将一组事件作为您的数据源,它可能会大大简化您的代码。

    【讨论】:

    • 感谢您的回复 phi,但是,对于您所说的使用 initWithEvent 初始化 eventDetailViewController 的位置,我感到很困惑?这不需要我在对象“事件”中有某种方法吗?我一直在尝试使用 performSegue 方法我已经用添加的方法更新了原始问题。通过 NSLog,我已经能够确定索引路径取决于我选择的单元格,哪个是好的。但是,我无法更改第二张幻灯片的内容?
    • 在尝试为您提供更多帮助之前,我建议您阅读this article,因为它涵盖了您要尝试做的事情的基础知识。
    • 抱歉!我以为我编辑了自己的问题,不小心编辑了你的答案。但是,我已经查看了那篇文章,因为我对 Objective-c 还是很陌生,请原谅我缺乏知识,但是从 UITableView 到 UICollectionView 的上下文有多适用?这篇文章本身也提到了我不太熟悉的 Swift。我有一个理论,关于辅助视图控制器中的 UILabel,我是否必须在 viewDidLoad 中初始化它们才能让它们的属性从以前的视图控制器 prepareForSegue 方法分配?
    • 感谢所有帮助 phi。我已经能够修改代码并让它工作:)
    • UITableView 和 UICollectionView 非常相似。请尽量忽略 Swift,专注于理解在“master”和“detail”视图控制器之间传递数据的概念。文章中的“Quote”类应该是您示例中的“Event”类。然后,将更容易理解如何填充视图控制器并将视图与模型分离。
    猜你喜欢
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多