Выбор вида коллекции и проблема DidDeselect - PullRequest
0 голосов
/ 14 октября 2019

Я пытаюсь изменить шрифт текста ячейки на didSelect и DidDeselect делегате представления коллекции, но это не работает нормально. Сначала я хочу, чтобы мой первый шрифт текста ячейки был жирным, и когда пользователь нажимает любую другую ячейку, он должен изменить шрифт этой ячейки, а оставшаяся ячейка возвращается к исходному шрифту. Это мой код,

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let balanceData = balanceArray[indexPath.row]
    self.recentTransactionArray.removeAll()
    let selectedCell = currencyCVC.cellForItem(at: indexPath)  as? CurrencyCVC
    //self.index = indexPath

    if indexPath != self.index
    {
        selectedCell?.amountLbl.font = UIFont.systemFont(ofSize:18, weight: .bold)
        selectedCell?.currencyLbl.font = UIFont.systemFont(ofSize:22, weight: .bold)
        selectedCell?.availableBalanceLbl.font = UIFont.systemFont(ofSize:11, weight: .bold)
        selectedCell?.depositLbl.font = UIFont.systemFont(ofSize:11, weight: .bold)
    }
    else
    {
        selectedCell?.amountLbl.font = UIFont.systemFont(ofSize:18, weight: .medium)
        selectedCell?.currencyLbl.font = UIFont.systemFont(ofSize:22, weight: .regular)
        selectedCell?.availableBalanceLbl.font = UIFont.systemFont(ofSize:11, weight: .medium)
        selectedCell?.depositLbl.font = UIFont.systemFont(ofSize:11, weight: .medium)
    }
}

Это мой делегат DidDeselect,

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    guard let cell = collectionView.cellForItem(at: indexPath) as? CurrencyCVC else {
        return
    }

    if indexPath != self.index
    {
        DispatchQueue.main.async {
            cell.amountLbl.font = UIFont.systemFont(ofSize:18, weight: .medium)
            cell.currencyLbl.font = UIFont.systemFont(ofSize:22, weight: .regular)
            cell.availableBalanceLbl.font = UIFont.systemFont(ofSize:11, weight: .medium)
            cell.depositLbl.font = UIFont.systemFont(ofSize:11, weight: .medium)
        }
    }
}

Теперь, когда я нажимаю на любую ячейку, она меняет текущий шрифт ячейки, но оставшийся шрифт ячейки не меняется на исходный. шрифт. Как я могу это сделать?

1 Ответ

0 голосов
/ 14 октября 2019

Попробуйте следующую логику, ее довольно просто выполнить

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
    cell.title.text = arrData[indexPath.item].name

    //  setting different fonts when cell is selected & when unselected
    cell.title.font = arrData[indexPath.item].isSelected ? UIFont.boldSystemFont(ofSize: 15) : UIFont.boldSystemFont(ofSize: 13)
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    //  here setting all other selections false
    arrData = arrData.compactMap{ return myModel(name: $0.name, isSelected: false) }

    //  then setting selected cell bit true
    arrData[indexPath.row].isSelected = true

    DispatchQueue.main.async {
        collectionView.reloadData()
    }
}
...