gpt4 book ai didi

ios - UiCollcetionViewCell 特定的 segue 连接 TableView 显示 TableView 内的特定内容

转载 作者:行者123 更新时间:2023-11-29 01:16:41 25 4
gpt4 key购买 nike

你好,我想问的问题是有没有一种方法可以在 Objective C 中创建这样的代码,它允许我在 UICollectionViewController 中选择一个 UICollectionViewCell 和将它链接到一个 TableView ,该 TableView 根据已选择的 UICollectionViewCell 显示 UICollectionViewCell 特定内容,而无需创建数百个 UITableViewController?

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

点击 Collection View 单元格显示表格 View 。

然后在 TableView 上...

如果点击 Collection View 是 dresses 显示 dresses TableView 。

这真的可能吗?

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

最佳答案

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

在 View Controller 中

.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

细节 View Controller

.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

关于ios - UiCollcetionViewCell 特定的 segue 连接 TableView 显示 TableView 内的特定内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35101860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com