【发布时间】:2019-03-11 06:19:26
【问题描述】:
您好,在我的应用程序中,我正在使用一个表格视图,每个单元格包含一个集合视图。当我在表格视图中选择一个单元格时,该单元格应以浅灰色突出显示,而前一个应更改为黑色,我正在执行如下操作。
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
collectionView.backgroundColor=[UIColor clearColor];
collectionViewTagValue=collectionView.tag;
nextFocusedIndex=indexPath;
selectedCollectionCellTag=collectionView.tag;
// Get previously selected collectionview tag value(Because we have many collectionvies in table)
NSIndexPath *previousPath=[NSIndexPath indexPathForRow:self->appdelegateObject.guideSelectionTag inSection:0];
// Get Tableview cell based on tag value - that tag value will be a tableview row number
DetailTableViewCell *cell = (DetailTableViewCell*)[self->guideDetailsTable cellForRowAtIndexPath:previousPath];
// Get indexpath of a selected cell in collection view
NSIndexPath *previouslySelectedCell=[NSIndexPath indexPathForRow:self->appdelegateObject.guideSelectionIndex inSection:0];
// Get collectionview cell based on indexpath value
MainCollectionViewCell *previousCell = (MainCollectionViewCell*)[cell.collection cellForItemAtIndexPath:previouslySelectedCell];
// Change celllabel background color to normal not highlight
previousCell.cellLable.backgroundColor=[UIColor colorWithRed:0.073 green:0.073 blue:0.073 alpha:1.0];
MainCollectionViewCell *currentCell = (MainCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
currentCell.cellLable.backgroundColor=[UIColor lightGrayColor];
}
使用上面的代码有时以前和当前的单元格都以浅灰色突出显示。即使我在 dispatch_main_queue 中进行了此更改,我观察到的行为也相同。谁能建议此功能的最佳方法。
注意:对于第一个单元格选择,我将 collectionview.tag 设置为 -1。
【问题讨论】:
-
正如您提到的那样,它有时会发生,它可能与响应有关。即当您单击代表调用表一或集合一的单元格时?
标签: ios uitableview uicollectionview uicollectionviewcell