您好我想问的问题是有没有办法在Objective C中创建这样的代码,这样我就可以在UICollectionViewCell
中选择一个UICollectionViewController
并将其链接到表格视图根据选择的UICollectionViewCell显示UICollectionViewCell
特定内容,而不创建数百个UITableViewControllers?
知道这不是代码,但这是我希望代码在可能的情况下实现的目的:
单击集合视图单元格连接显示表视图。
然后在表格视图中......
如果点击的集合视图是礼服展示礼服表视图。
肯定这有可能吗?此外,如果可能的话,我希望代码以某种方式将事物分组,因为这将在大规模上完成我有106个集合视图单元,需要链接到表视图,每个表视图需要包含至少30个TableViewCell。 / p>
答案 0 :(得分:1)
我尝试了你的问题以获得解决方案。最后我成功地得到了解决方案.Below代码完美无缺。
在ViewController中
·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
DetailViewController
·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