我知道您会自动尝试说这与通过视图控制器页面发布数据进行传递是相同的,但这不是因为在这种情况下这是非常不同的,而且是何时引入UIColletionView的,因为与之相比似乎几乎没有任何信息UITableView。

因此,这里的问题是我有一个UICollectionView,它具有3个名为A B和C图片的单元格,如下所示:

ios - 将UICollectionViewCell连接到简单的View Controller标题-LMLPHP

当我单击UiCollectionViewCell“ A”时,我希望将其发送到一个新的空白ViewController,该空白实现了UICollectionViewCell标签(其中显示字母A)到我的UIViewController标题。

我当前的代码如下,我已经知道我缺少我的-void准备序列代码,那是我应该使用的对吗?而且我知道我应该让UICollcetionView项目选择代码,但这就是我正在努力设置的内容,并且我一生都在搜索互联网,并且这里大约有300篇帖子介绍了如何执行此操作,没有记录否则我的搜索查询肯定显然缺少一些重要的信息,我可以整日使用表视图来完成此操作,但从未使用过comllcetionview,而之前我曾假设基于表视图的先前代码该代码将是相似的,但我只是找不到如何做我想做的事。

GroupsViewController.h

#import <UIKit/UIKit.h>

@interface GroupsViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *GroupsCollectionView;

- (IBAction)cellToggleAction:(id)sender;


@end


GroupsViewController.m

#import "GroupsViewController.h"
#import "GroupsHomeViewController.h"
#import "CustomCell.h"

@interface GroupsViewController ()
{
    NSArray *arrayOfImages;
    NSArray *arrayOfDescriptions;
}

@end

@implementation GroupsViewController
{
    NSString *reuseIdentifier;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[self GroupsCollectionView]setDataSource:self];
    [[self GroupsCollectionView]setDelegate:self];
    reuseIdentifier= @"SmallIcon";

    arrayOfImages = [[NSArray alloc]initWithObjects:@"A.png",@"B.png",@"C.png",nil];

    arrayOfDescriptions = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",nil];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [arrayOfDescriptions count];
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    [[cell IconImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];
    [[cell IconLabel]setText:[arrayOfDescriptions objectAtIndex:indexPath.item]];

    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    //Dispose of any resources that can be recreated.
}

// Toggle View Button
- (IBAction)cellToggleAction:(id)sender {

    if([reuseIdentifier isEqualToString:@"SmallIcon"]){
        reuseIdentifier=@"ListView";
        [sender setImage:[UIImage imageNamed:@"LargeIcon"]];
    }
    else if
        ([reuseIdentifier isEqualToString:@"ListView"]){
        reuseIdentifier=@"LargeIcon";
        [sender setImage:[UIImage imageNamed:@"SmallIcon"]];
    }
    else if
        ([reuseIdentifier isEqualToString:@"LargeIcon"]){
        reuseIdentifier=@"SmallIcon";
        [sender setImage:[UIImage imageNamed:@"ListView"]];
    }

    [self.GroupsCollectionView reloadData];
}

//Toggled Cell Sizes
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    CGSize cellSize;

    if([reuseIdentifier isEqualToString:@"SmallIcon"])
        cellSize = CGSizeMake(100, 130);
    else if
        ([reuseIdentifier isEqualToString:@"ListView"])
        cellSize = CGSizeMake(320, 50);
    else if
        ([reuseIdentifier isEqualToString:@"LargeIcon"])
        cellSize = CGSizeMake(320, 350);

    return cellSize;
}

@end


GroupsHomeViewController.h

#import <UIKit/UIKit.h>

@interface GroupsHomeViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *logoImage;
@property (strong, nonatomic) IBOutlet UILabel *groupLabel;

@end


GroupsHomeViewController.m

#import "GroupsHomeViewController.h"

@interface GroupsHomeViewController ()

@end

@implementation GroupsHomeViewController

-(void)viewDidLoad{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


CustomCell.h

#import <UIKit/UIKit.h>

@interface CustomCell : UICollectionViewCell

@property (weak, nonatomic) IBOutlet UIImageView *IconImage;

@property (weak, nonatomic) IBOutlet UILabel *IconLabel;

@end


CustomCell.m

#import "CustomCell.h"

@implementation CustomCell

@end


如果您想获取更多信息以进一步了解我要完成的工作,请在下面发表评论,并在此先感谢您的答复。

最佳答案

您需要在“视图”控制器中创建一个属性,在选择“收藏夹”视图单元时要导航至该属性。

因此,在您的视图控制器的.h文件中添加

@property (nonatomic, strong) NSString* titleText;


并为此属性创建一个自定义设置器。这是我们设置视图控制器标题的地方。

在您的视图控制器的.m文件中添加

- (void)setTitleText:(NSString *)titleText {

    _titleText = titleForNextVC;

    // Set Title of your ViewController
    self.title = _titleText;
}


现在,您需要将字符串从CustomCell中的GroupsViewController传递到下一个视图控制器。

为此,在GroupsViewController.m中实现collectionView:didSelectItemAtIndexPath:方法

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    CustomCell* cell = [self collectionView:collectionView cellForItemAtIndexPath:indexPath];
    _titleForNextVC = cell.IconLabel.text;
}


在这里,titleForNextVC只是GroupsViewController.m类扩展中的NSString属性。

@interface GroupsViewController ()
{
    NSArray *arrayOfImages;
    NSArray *arrayOfDescriptions;

    NSString* _titleForNextVC;
}


现在,只需将此字符串传递给titleText中下一个视图控制器的prepareForSegue:sender:属性

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:yourSegueIdentifier]) {

        // Replace YourNextViewController with actual class of your UIViewController
        YourNextViewController *vc = (YourNextViewController *)segue.destinationViewController;
        vc.titleText = _titleForNextVC;
    }
}


您完成了。

关于ios - 将UICollectionViewCell连接到简单的View Controller标题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35242576/

10-09 10:21