【发布时间】:2017-07-25 06:07:31
【问题描述】:
[![在此处输入图片描述][1]][1]
我有一个表格视图。在该表格视图中,我需要在不同部分显示集合视图。我采用原型类型表格视图单元格并显示集合视图。我对该表格视图中的不同部分使用相同的集合视图。当我单击集合视图单元格时,我得到了集合视图部分和 indexPath,但是如何获取 tableview 部分和 indexPath 行?
#pragma mark- UITableView Delegate & Datasource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section == 0) {
return 1;
}
else if (section == 1) {
return 1;
}
else if (section == 2) {
return 1;
}
else
return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
return 128;
}
else if (indexPath.section == 1) {
return 321;
}
else if (indexPath.section == 2) {
return 321;
}
else
return 262;
}
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSString *sectionName;
switch (section) {
case 0:
sectionName = @"";
break;
case 1:
sectionName = @"Features Products";
break;
case 2:
sectionName = @"New Products";
break;
case 3:
sectionName = @"";
break;
default:
break;
}
return sectionName;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
FeaturesTableViewCell *featureCell;
CustomTableViewCell *cell;
CustomTableViewCell *normalCell;
FashionTableViewCell *fashionCell;
if (indexPath.section==0) {
featureCell = [tableView dequeueReusableCellWithIdentifier:@"FeaturesCell"];
if (featureCell==nil) {
featureCell = [[FeaturesTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FeaturesCell"];
}
return featureCell;
}
else if (indexPath.section==1) {
cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell"];
if (cell==nil)
{
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomTableViewCell"];
}
cell.superIndexPath = indexPath;
return cell;
}
else if (indexPath.section==2) {
normalCell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell"];
if (normalCell==nil)
{
normalCell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomTableViewCell"];
}
cell.superIndexPath = indexPath;
return normalCell;
}
else{
fashionCell = (FashionTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"FashionTableViewCell"];
if (fashionCell==nil)
{
fashionCell = [[FashionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FashionTableViewCell"];
}
return fashionCell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"UITableView Selected index----%ld",indexPath.row);
}
#import <UIKit/UIKit.h>
#import <Foundation/NSObject.h>
#import <Foundation/Foundation.h>
@interface CustomTableViewCell : UITableViewCell<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong ,nonatomic) NSIndexPath *superIndexPath;
@end
#import "CustomTableViewCell.h"
@implementation CustomTableViewCell
@synthesize superIndexPath;
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#pragma mark- UICollectionView Delegate & Datasource methods
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
NSUInteger numberOfSections= 0;
numberOfSections = 1;
return numberOfSections;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
NSUInteger numberOfItems= 0;
numberOfItems = 6;
return numberOfItems;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
UIImageView *imgView = (UIImageView *) [cell viewWithTag:1000];
imgView.image = [UIImage imageNamed:@"slideImage1"];
[cell.contentView addSubview:imgView];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"Selected superIndexPath section---------%ld",(long)superIndexPath.section);
NSLog(@"Selected superIndexPath.row---------%ld",superIndexPath.row);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 0.0f;
}
- (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 0.0f;
}
@end
【问题讨论】:
-
你为什么在tableViewCell里面使用collectionView,而不是collectionView with section?
-
@Balasubramanian 我对每个 tableview 部分都有不同的设计
-
最简单的方法是在cellForRowIndexPath:方法中将tableView的indexpath保存在collectionView中,这样就可以在collectionView的action中获取indexpath
-
你的单元应该实现一个允许你的视图控制器成为单元代表的协议。然后该单元格将成为集合视图的委托。当collection view中的item被选中时,可以调用tableview中的delegate方法,传递collection item和cell。然后,您可以确定单元格行。请参阅stackoverflow.com/questions/28659845/… 了解类似的方法(它是 Swift,但目标 C 方法是相同的)
标签: ios objective-c iphone uitableview uicollectionview