2016-08-30 58 views
1

我正在UITableViewCell中使用UICollectionView。我完成了添加內容,我可以看到內部UITableViewCell在UITableViewCell中檢測UICollectionView上的點擊

但是現在我不知道如何檢查哪個cellUICollectionView是在UITableView的哪一行裏面被點擊的。

所以,如果有人知道如何識別它會有幫助。

在此先感謝。

#pragma mark 
#pragma mark - UITableViewDelegate and UITableViewDatasource 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return 15; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    CellForCollectionView *cell = [tableView dequeueReusableCellWithIdentifier:@"CellForCollectionView"]; 

    if (cell == nil) { 
     cell = [[CellForCollectionView alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellForCollectionView"]; 
    } 

    cell.categoryCollectionView.delegate = self; 
    cell.categoryCollectionView.dataSource = self; 

    [cell.categoryCollectionView registerNib:[UINib nibWithNibName:@"CollectionCellForCategory" bundle:nil] forCellWithReuseIdentifier:@"CollectionCellForCategory"]; 
    cell.lblCategoryName.text = [NSString stringWithFormat:@" Category %d",indexPath.row]; 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    return cell; 
} 






#pragma mark 
#pragma mark - UIcollectionViewDelegate and UIcollectionViewDatasource 
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ 
    return 1; 
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 
    return 15; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    CollectionCellForCategory *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCellForCategory" forIndexPath:indexPath]; 
    cell.imagCategory.layer.borderColor = [[UIColor blackColor]CGColor]; 
    cell.imagCategory.layer.borderWidth = 1.0f; 
    return cell; 
} 


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ 
    NSLog(@"%ld",(long)indexPath.row); 
    NSLog(@"%ld",(long)indexPath.section); 
} 
+0

選擇不適用於收集視圖嗎? – Himanshu

+0

是否選擇正在工作,但使用此如何才能識別UITableView的行號。 –

+0

你可以給我你的用戶界面的想法。 – Himanshu

回答

0
  1. 你也可以繼承UICollectionView,並添加一個變量來跟蹤tableViewCell
  2. 在你tableViewCell的indexPath改變的CollectionView類型定製的CollectionView
  3. 設置在「的cellForRowAtIndexPath」方法變量UITableViewDatasource

定製的CollectionView:

@interface IndexedCollectionView : UICollectionView 
@property (nonatomic, strong) NSIndexPath* parentIndexpath; 
@end 

您的自定義tableViewCell(約)

@interface CellForCollectionView : UITableViewCell 
@property (nonatomic, weak) IBOutlet IndexedCollectionView* categoryCollectionView; 
@end 

UITableViewDataSource方法實現:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    CellForCollectionView *cell = [tableView dequeueReusableCellWithIdentifier:@"CellForCollectionView"]; 

    if (cell == nil) { 
     cell = [[CellForCollectionView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellForCollectionView"]; 
    } 

    cell.categoryCollectionView.delegate = self; 
    cell.categoryCollectionView.dataSource = self; 

    [cell.categoryCollectionView registerNib:[UINib nibWithNibName:@"CollectionCellForCategory" bundle:nil] forCellWithReuseIdentifier:@"CollectionCellForCategory"]; 
    cell.lblCategoryName.text = [NSString stringWithFormat:@" Category %d",indexPath.row]; 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.categoryCollectionView.parentIndexpath = indexPath; 
    return cell; 
} 
+0

嘿我有一個小疑問請問 實際上,由於細胞的可重用性,它不會更新UITableView中其他單元的「parentIndexpath」,所以如果你知道的話,你可以給我建議解決方案。謝謝。 –

-1

很簡單。添加標籤的UITableView每個單元格中的「cellForRowAtIndexPath:」法即

cell.tag = indexPath.row 

同樣適用於UICollectionView

讓裏面的CollectionViewCell說你有一個的tableView 2行和分別有2個collectionViews。

UITableView的第1行將具有標記0,而另一個將具有標記1

現在當上的CollectionView內collectionViewCell用戶點擊這裏面是一個UITableViewCell

現在,如果您單擊第一個的UITableViewCell內的CollectionView的第一collectionViewCell

你將得到的細胞標記爲0,正如你已經知道,self.tag即是tableViewCell是0

現在,如果你點擊第二的UITableViewCell

內的CollectionView的第一collectionViewCell

你將得到的細胞標記爲0,因爲你已經知道self.tag即是tableViewCell是1

我認爲這是你needed.Do讓我知道,如果它是你清楚..謝謝:)