当在 UITableviewCell 中使用 UICollection 视图时,未调用 UICollectionView 委托方法 "didSelectItemAtIndexPath"
UICollectionView delegate method "didSelectItemAtIndexPath" not called when UICollection view used in UITableviewCell
我在 UITableviewCell 中使用了 UICollectionView,一切正常,仍在加载集合视图并显示其单元格,但加载集合视图后,我无法点击集合视图单元格时获取"didSelectItemAtIndexPath"集合视图的委托方法。
我的 Table 视图具有静态单元格,我将集合视图放在该单元格中
同时在其上定义委托和数据源
这是我的代码,调用了数据源方法但未调用委托
#pragma mark - collectionView data source
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 10;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collCell" forIndexPath:indexPath];
UIImageView *imgDocCell = (UIImageView *)[cell viewWithTag:100];
imgDocCell.image = self.imgFileToChart;
cell.layer.borderWidth=1.0f;
cell.layer.borderColor=commonThemeColor.CGColor;
return cell;
}
#pragma mark - collectionView delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@",indexPath);
}
删除集合视图的出口并委托和数据源,参见OUTPUT HERE
你应该这样写你的代码
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
// Here tableviewcell inside one collection view create with tag 10
UICollectionView *coll =(UICollectionView*)[cell viewWithTag:10];
coll.delegate = self;
coll.dataSource = self;
return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 3;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell2" forIndexPath:indexPath];
// in collectionview storyboard one UIImageview with tag 20
UIImageView *imgDocCell = (UIImageView *)[cell1 viewWithTag:20];
imgDocCell.image =[UIImage imageNamed:@"add.jpeg"];
return cell1;
}
#pragma mark - collectionView delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@",indexPath);
}
我在 UITableviewCell 中使用了 UICollectionView,一切正常,仍在加载集合视图并显示其单元格,但加载集合视图后,我无法点击集合视图单元格时获取"didSelectItemAtIndexPath"集合视图的委托方法。
我的 Table 视图具有静态单元格,我将集合视图放在该单元格中
同时在其上定义委托和数据源
这是我的代码,调用了数据源方法但未调用委托
#pragma mark - collectionView data source
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 10;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collCell" forIndexPath:indexPath];
UIImageView *imgDocCell = (UIImageView *)[cell viewWithTag:100];
imgDocCell.image = self.imgFileToChart;
cell.layer.borderWidth=1.0f;
cell.layer.borderColor=commonThemeColor.CGColor;
return cell;
}
#pragma mark - collectionView delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@",indexPath);
}
删除集合视图的出口并委托和数据源,参见OUTPUT HERE
你应该这样写你的代码
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
// Here tableviewcell inside one collection view create with tag 10
UICollectionView *coll =(UICollectionView*)[cell viewWithTag:10];
coll.delegate = self;
coll.dataSource = self;
return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 3;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell2" forIndexPath:indexPath];
// in collectionview storyboard one UIImageview with tag 20
UIImageView *imgDocCell = (UIImageView *)[cell1 viewWithTag:20];
imgDocCell.image =[UIImage imageNamed:@"add.jpeg"];
return cell1;
}
#pragma mark - collectionView delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@",indexPath);
}