func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "DateTableViewCell") as! DateTableViewCell
cell.backgroundColor = UIColor.clear
cell.dayOfWeekLabel.text = currentDayOfWeek
cell.monthDateLabel.text = "\(currentMonth!) \(currentDay!)"
cell.yearLabel.text = currentYear
return cell
} else if indexPath.row == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "MonthWeekTableViewCell") as! MonthWeekTableViewCell
cell.selectionStyle = .none
cell.backgroundColor = UIColor.clear
cell.monthYear = (currentMonth,currentYear)
return cell
} else if indexPath.row == 2{
let cell = tableView.dequeueReusableCell(withIdentifier: "MonthWeekTableViewCell") as! MonthWeekTableViewCell
cell.selectionStyle = .none
cell.backgroundColor = UIColor.clear
cell.monthYear = ("May",currentYear)
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "MonthWeekTableViewCell") as! MonthWeekTableViewCell
cell.selectionStyle = .none
cell.backgroundColor = UIColor.clear
cell.monthYear = ("June",currentYear)
return cell
}
}
当我向上滚动表格视图时,包含4月和5月月份的内容的单元格消失了。但是,加载视图时,我可以看到4月和5月的内容。但是,一旦我开始滚动到底部并向上滚动,就再也看不到April内容了。
单元格代码:
import UIKit
class MonthWeekTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var monthLabel: UILabel!
@IBOutlet weak var dayCollectionView: UICollectionView!
var monthYear: (String,String) = ("","") {
didSet {
monthLabel.text = self.monthYear.0
guard let _maxDate = DateFormat.returnNumberOfDaysInMonth(month: self.monthYear.0, year: self.monthYear.1) else {fatalError("Tuple Error")}
maxDate = _maxDate
firstDay = DateFormat.getFirstOfMonth(year: self.monthYear.1, month: self.monthYear.0)
}
}
var firstDay: Int = 0 {
didSet {
dayCollectionView.reloadData()
}
}
var dayNumber: Int = 1
var maxDate: Int = 30
override func awakeFromNib() {
super.awakeFromNib()
dayCollectionView.delegate = self
dayCollectionView.dataSource = self
dayCollectionView.register(UINib(nibName: "DayCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "DayCollectionViewCell")
dayCollectionView.backgroundColor = UIColor.clear
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 7
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DayCollectionViewCell", for: indexPath) as! DayCollectionViewCell
if (indexPath.section != 0 || (indexPath.row + 1) >= firstDay) && dayNumber <= maxDate{
cell.dayLabel.text = String(dayNumber)
dayNumber += 1
cell.backgroundColor = UIColor.clear
} else {
cell.isHidden = true
}
return cell
}
}
答案 0 :(得分:0)
要在两次使用之间重置单元格的状态,可以覆盖here中记录的单元格的prepareForReuse
功能。当您调用dequeueReusableCell
时,该函数将在单元格上被调用,因此您可以使用该函数使该单元格返回基本状态,然后再在cellForItemAt
函数中对其进行重新配置。
override func prepareForReuse() {
super.prepareForReuse() // make sure you include this
dayNumber = 1
}
答案 1 :(得分:0)
我意识到我的代码中有两个问题。 1)如@rpecka所述,我需要重写函数prepareForReuse
override func prepareForReuse() {
super.prepareForReuse()
dayNumber = 1
}
2)我在使用按钮时也需要取消隐藏
cell.isHidden = false