CollectionViewCell使用上一个单元格标题和图像

时间:2016-02-02 20:57:16

标签: ios objective-c segue collectionview

我现在已经浏览了整个网站3个小时了,如果没有参考手动将所有106个集合视图单元格与segue连接到另一个ViewController,我无法找到如何做到这一点。

我希望能够点击我的CustomCell(我们将其称为A)当我点击A我想要打开一个新的viewController,其中Page Title为A,我在CustomCell中设置的图像是视图控制器页面上UIImage中的Page徽标。

我知道这与在ViewControllers之间传递信息有关,我不知道如何做到这一点。所以我很困惑。

我现在只使用Xcode已经一个月了,我需要它来为我的学徒项目所以如果有人可以帮助我会非常有帮助。

我的当前代码如下,如果有人非常友好以帮助您,请您评论您的代码,以便我进一步理解,因为我不只是想复制和粘贴代码并从中学到任何东西。

非常感谢提前。

ViewController.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", nil];

    arrayOfDescriptions = [[NSArray alloc]initWithObjects:@"A",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)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    [self performSegueWithIdentifier:@"customSegue" sender:indexPath];
}

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

    NSIndexPath *indexPath = (NSIndexPath *)sender;
    UIImage *imageToShow = [UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]];
    NSString *titleToShow = [arrayOfDescriptions objectAtIndex:indexPath.item];
    GroupsHomeViewController * destination = (GroupsHomeViewController *)segue.destinationViewController;
    destination.groupLabel = *titleToShow; //assigning to UILabel form incompatible type NSString
    destination.logoImage = *imageToShow; // UIImageView from incompatible type UImage
}

- (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.m,其中组视图控制器付费转到。

#import "GroupsHomeViewController.h"

@interface GroupsHomeViewController ()

@end

@implementation GroupsHomeViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    self.groupLabel.text = self.groupLabel; //incompatible pointertypes assigning to NSSstring
    self.logoImage.image = self.logoImage; //incompatible pointer types assigning to NSSstring

    // Do any additional setup after loading the view.
}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

CustomeCell.h

#import <UIKit/UIKit.h>

@interface CustomCell : UICollectionViewCell

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

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

@end

1 个答案:

答案 0 :(得分:0)

确定。这是一个非常大的问题。但首先,您需要为segue的目标创建一个自定义VC子类。你绝对不应该有106个不同的segue。特别是因为听起来他们基本上都会走向同一个地方。可以显示图像和名称的View控制器。在故事板中设置这个新的VC,并从viewController(不是单元格)(从集合VC顶部的黄色圆圈拖动)到新VC创建一个segue。并称之为customSegue

设置类似的子类:

    @interface SelectedInfoViewController : UIViewController
    @property (strong, nonatomic) NSString *selectedInfoName;
    @property (strong, nonatomic) UIImage *selectedInfoImage;
    @end

    @implementation SelectedViewController


-(void)viewDidLoad{
    [super viewDidLoad];
    self.labelForTitle.text = self.selecteInfoName;
    self.imageViewForImage.image = self.selecteInfoImage;
}

    @end

然后你会添加到你的集合视图控制器。

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

    [self performSegueWithIdentifier:@"customSegue" sender:indexPath];
}

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

    NSIndexPath *indexPath = (NSIndexPath *)sender;
     UIImage *imageToShow = [UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]];
    NSString *titleToShow = [arrayOfDescriptions objectAtIndex:indexPath.item]];
    SelectedInfoViewController * destination = (SelectedInfoViewController *)segue.destinationViewController;
    destination.selectionInfoName = titleToShow;
    destination.selectionInfoImage = imageToShow;
}

希望这有一定道理。如果您需要更多帮助,请告诉我。

根据您的错误:

destination.groupLabel = *titleToShow; //assigning to UILabel form incompatible type NSString
destination.logoImage = *imageToShow; // UIImageView from incompatible type UImage

self.groupLabel.text = self.groupLabel; //incompatible pointertypes assigning to NSSstring
self.logoImage.image = self.logoImage; //incompatible pointer types assigning to NSSstring

这是您要分配给彼此的两件事。 所以我突出显示你需要设置:

  @interface SelectedInfoViewController : UIViewController
        @property (strong, nonatomic) NSString *selectedInfoName;
        @property (strong, nonatomic) UIImage *selectedInfoImage;
but also we need:
        @property (strong, nonatomic) IBOutlet UILabel *infoLabel;
        @property (strong, nonatomic) IBOutlet UIImageView *infoImage;
        @end

SOOOO。在segue中,您应该设置selectedInfoName和selectedInfoImage。然后在viewDidLoad中,您应该将infoLabel.text属性设置为传递的selectedInfoName,并将infoImage.image属性设置为传递的selectedInfoImage。

哦,正如我之前提到的那样,你需要链接infoLabel&amp; infoImage到故事板中各自的对应部分。