UiCollcetionViewCell 特定 segue 用于连接 TableView 显示 TableView 中的


嗨,我想问的问题是,有

没有一种方法可以在目标C中创建这样的代码,允许我在UICollectionViewController中选择单个UICollectionViewCell并将其链接到一个表视图,该视图根据所选的UICollectionViewCell显示UICollectionViewCell特定内容,而无需创建数百个UITableViewController?

知道这不是代码,但如果可能的话,这就是我希望代码实现的目标:

单击"集合"视图时,单元格连衣裙显示表视图。

然后在表视图上...

如果"收藏"视图单击"是"连衣裙显示连衣裙表视图"。

这当然可能吗?

此外,如果可能的话,我希望代码以某种方式将事物组合在一起,因为这将大规模完成,我有 106 个集合视图单元格需要链接到表格视图,每个单元格至少需要包含 30 个 TableViewCells。

我尝试了您的问题以获得解决方案。最后我成功地得到了解决方案。下面的代码完美运行。

在视图控制器中

.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
@property (strong, nonatomic) IBOutlet UICollectionView *collectionViewSelection;
@end

.m

#import "ViewController.h"
#import "CustomCollectionViewCell.h"
#import "DetailViewController.h"
@interface ViewController ()
{
   NSMutableArray *arrayCollectionView;
   NSMutableArray *imgArray;
   NSMutableArray *lblArray;
}
@end
@implementation ViewController
@synthesize collectionViewSelection;
- (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
 imgArray = [NSMutableArray arrayWithObjects:
            [UIImage imageNamed:@"casual.png"],
            [UIImage imageNamed:@"checked.png"],
            [UIImage imageNamed:@"collar.png"],
            [UIImage imageNamed:@"formal.png"],
            [UIImage imageNamed:@"jean.png"],
            [UIImage imageNamed:@"neck.png"],
            [UIImage imageNamed:@"pant.png"],nil];
 lblArray = [NSMutableArray arrayWithObjects:@"casual",@"checked",@"collar",@"formal",@"jean",@"neck",@"pant", nil];
 UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
 flowLayout.scrollDirection =  UICollectionViewScrollDirectionVertical;
 UINib *cellNib = [UINib nibWithNibName:@"CustomCollectionViewCell" bundle:nil];
 [collectionViewSelection registerNib:cellNib forCellWithReuseIdentifier:@"customCollectionCell"];
}
- (void)didReceiveMemoryWarning {
 [super didReceiveMemoryWarning];
 // Dispose of any resources that can be recreated.
}

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

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *cellIdentifier = @"customCollectionCell";
   CustomCollectionViewCell *cell = (CustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
   NSLog(@"The current indexPath row is - %ld",(long)indexPath.row);
   cell.img_Collection.image = [imgArray objectAtIndex:indexPath.row];
   cell.label_Collection.text = [lblArray objectAtIndex:indexPath.row];
   cell.tag = indexPath.row;
   return cell;
}
 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{
  return CGSizeMake(260, 176);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
   NSLog(@"The touched index path os collection cell item row is - %ld",(long)indexPath.row);
   DetailViewController *detailsVC = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
   detailsVC.stringLabeldata = [lblArray objectAtIndex:indexPath.row];
   detailsVC.imageData = [imgArray objectAtIndex:indexPath.row];
   [self.navigationController pushViewController:detailsVC animated:YES];
}
@end

详细视图控制器

.h

#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableViewDressesData;
@property (strong, nonatomic) UIImage *imageData;
@property (strong, nonatomic) NSString *stringLabeldata;
- (IBAction)actionBack:(id)sender;
@end

.m

 #import "DetailViewController.h"
 #import "CustomTableViewCell.h"
 @interface DetailViewController ()
 @end
 @implementation DetailViewController
 @synthesize tableViewDressesData;
 @synthesize imageData,stringLabeldata;
 - (void)viewDidLoad {
   [super viewDidLoad];
   // Do any additional setup after loading the view from its nib.
   NSLog(@"The labeldata is -%@",stringLabeldata);
   [tableViewDressesData registerNib:[UINib nibWithNibName:@"CustomTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
   [tableViewDressesData reloadData];
}
- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"];
   if (cell == nil) {
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil];
     cell = [nib objectAtIndex:0];
   }
   cell.imgViewDetail.image = imageData;
   cell.labelDetail.text = stringLabeldata;
   return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   return 188;
}
- (IBAction)actionBack:(id)sender
{
  [self.navigationController popToRootViewControllerAnimated:YES];
}
@end

最新更新