滚动加载时的CollectionView单元格数据,但didDeselectItemAt中的错误可选nil值

时间:2018-09-27 12:00:28

标签: swift collectionview

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Day_DateUICollectionViewCell", for: indexPath) as! Day_DateUICollectionViewCell

    cell.dayLabel.text = Util.getDateFormatter(date_api: self.avalabilityDates?[indexPath.row].date ?? "", dateType: "daysInWord")

     cell.dayNoButton.setTitle(Util.getDateFormatter(date_api: self.avalabilityDates?[indexPath.row].date ?? "", dateType: "days"), for: UIControlState.normal)


    if selectedCell.contains(indexPath) {
        cell.dayNoButton.layer.cornerRadius = cell.dayNoButton.frame.size.width/2
        cell.dayNoButton.backgroundColor = UIColor.red
        cell.dayNoButton.setTitleColor(UIColor.white, for: .normal)
    }
    else {
        cell.dayNoButton.backgroundColor = nil
        cell.dayNoButton.setTitleColor(UIColor.black, for: .normal)
    }
 return cell
}


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

  let cell = collectionView.cellForItem(at: indexPath) as! Day_DateUICollectionViewCell

  selectedCell.append(indexPath)

  cell.dayNoButton.layer.cornerRadius = cell.dayNoButton.frame.size.width/2
  cell.dayNoButton.backgroundColor = UIColor.red
  cell.dayNoButton.setTitleColor(UIColor.white, for: .normal)

dateLabel.text = Util.getDateFormatter(date_api: self.avalabilityDates?[indexPath.row].date ?? "", dateType: "")


self.getAvailabilityTimes(timeZone: Util.getTimeZone(), selectedMonth: Util.getTodayDate(), selectedDate:self.avalabilityDates?[indexPath.row].date ?? "")
}


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

       let cell = collectionView.cellForItem(at: indexPath) as! Day_DateUICollectionViewCell
        if selectedCell.contains(indexPath) {
          selectedCell.remove(at: selectedCell.index(of: indexPath)!)
            cell.dayNoButton.backgroundColor = nil
            cell.dayNoButton.setTitleColor(UIColor.black, for: .normal)
           }
        }

0 个答案:

没有答案