collectionView를 사용하여 캘린더를 만드는 연습을하고 있습니다.collectionView 셀이 reloadData() 후에 무작위로 강조 표시됩니다.
내 구현은 간단합니다. 지금은 주로 https://github.com/Akhilendra/calenderAppiOS/tree/master/myCalender2입니다. 간단한 캘린더 만 이미지합니다.
사용자가 원하는 날짜를 선택하고 셀을 선택한 후 약간의 경계선을 만들도록하여 몇 가지 발전을 이루려고합니다.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CustomCell
cell.backgroundColor=UIColor.clear
if indexPath.item <= firstWeekDayOfMonth - 2 {
print(firstWeekDayOfMonth)
cell.isHidden=true
} else {
let calcDate = indexPath.row-firstWeekDayOfMonth+2
cell.isHidden=false
cell.dateLabel.text="\(calcDate)"
if calcDate < todaysDate && currentYear == presentYear && currentMonthIndex == presentMonthIndex {
cell.isUserInteractionEnabled=true
cell.dateLabel.textColor = UIColor.darkGray
} else {
cell.isUserInteractionEnabled=true
cell.dateLabel.textColor = UIColor.black
}
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
addToList.append(indexPath)
//dayCollectionView.deselectItem(at: indexPath, animated: true)
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.borderWidth = 2.0
cell?.layer.borderColor = UIColor.gray.cgColor
}
지금 재사용 기능 때문에 선택한 셀이 내 collectionView 주위로 점프됩니다. 이 문제를 해결하는 방법은 reloadData를 사용하지 않는 것이 좋지만, 다음 달로 전달할 때 collectionView 전체를 플러시해야합니다.
누군가 나를 도와 줄 수 있습니까? 사용자가 다음 달로 이동하고 선택한 셀을 사용자가 선택한 시점에 유지하면 선택한 셀을 지우고 싶습니다.
미리 감사드립니다.
선택한 셀에 대해 부울 검사를 유지할 수 있습니다. –
@ user3431800 : 아래의 답변을보십시오 –