将UICollectionViewCell连接到简单的View Controller Title

时间:2016-02-06 15:13:29

标签: ios objective-c uicollectionview

我知道你会自动尝试说这与通过视图控制器页面帖子传递数据是一样的,这不是因为在这种情况下它是非常不同的,什么时候引入UIColletionView因为似乎几乎没有任何信息它与UITableView相比。

因此,我有一个UICollectionView的问题,其中有3个单元名为A B和C图片,如下所示:

Curent layout of CollectionViewControllers

当我点击UiCollectionViewCell" A"我希望它能发送给我一个新的空白ViewController,它实现了UICollectionViewCell标签(显示字母A)到我的UIViewController标题。

我当前的代码如下,我已经知道我错过了我的-void准备segue代码,这就是我应该使用的权利吗?我知道我应该有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

如果您希望了解更多信息以便进一步了解我想要完成的工作,请不要犹豫,在下面发表评论,并提前感谢您的任何答案。

1 个答案:

答案 0 :(得分:1)

您需要在View控制器中创建一个属性,以便在选择Collection视图单元格时导航到该属性。

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

@property (nonatomic, strong) NSString* titleText;

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

在视图控制器的 .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;
    }
}

你已经完成了。