【问题标题】:How to avoid getting highlighted two cells in a tableview?如何避免在表格视图中突出显示两个单元格?
【发布时间】: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


【解决方案1】:

实施了以下方法来解决您的问题:

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

    let yourCell = tableView.cellForRow(at: indexPath)

    yourCell?.backgroundColor = UIColor.black
}

这段代码在 Swift 中,你需要把它转换成 Objective-C。

此方法调用您选择的上一个单元格,它正在取消选择。

【讨论】:

    【解决方案2】:
     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let cell = tableView.cellForRow(at: indexPath)
            cell?.contentView.backgroundColor = UIColor.red
        }
    
        func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
            let cell = tableView.cellForRow(at: indexPath)
            cell?.contentView.backgroundColor = UIColor.lightGray
        }
    

    【讨论】:

      【解决方案3】:
      //add condition in tableView delegate in didselect or diddeSelect, check index.section..
          //for selected cell
          -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      
               UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
      // check indexPath.Section here
      if (!indexPath.section) == 0{ 
               cell.contentView.backgroundColor = [UIColor yellowColor];
      }
      
          }
      
          //for other cells
          -(void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
      // check indexPath.Section here
      if (!indexPath.section) == 0{ 
             cell.contentView.backgroundColor = [UIColor whiteColor];
            }
          }
      // hope its works for you!
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-18
        • 1970-01-01
        • 1970-01-01
        • 2023-01-04
        • 2020-09-08
        • 1970-01-01
        相关资源
        最近更新 更多