我在我的项目中使用JTAppleCalendar
,你可以在这里看到它 - > https://github.com/patchthecode/JTAppleCalendar。但是当我想改变一些细胞中的背景颜色给我带来很大的问题时,当我过去和未来几个月时,一些细胞背景颜色会发生变化?
怎么可能?我该如何解决?我只想改变;示例;
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName:"Mains")
let predicate = NSPredicate (format:"date = %@",freshdate)
fetchRequest.predicate = predicate
if let result = try? context.fetch(fetchRequest) as! [Mains] {
for object in result {
if(object.user! == "" < freshdate) {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "f7bca6")
} else if(object.userme! == "") {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "f7bca6")
} else {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "ffffff")
}
}
}
一个人,但是当我再转一个月时,在日历中看到一些细胞背景发生了变化。
下图显示应用程序打开时为true。
但是,当我过去的上个月或下个月改变了下面的一些细胞背景。这是错误的。我不想改变它。
我的代码在下面,我可以搞错?
@IBOutlet weak var calendarView: JTAppleCalendarView!
let kStartDate = "2016-01-01"
let kEndDate = "2049-12-31"
var numberOfRows = 6
let formatter = DateFormatter()
var myCalendar = Calendar(identifier: .gregorian)
var generateInDates: InDateCellGeneration = .forAllMonths
var generateOutDates: OutDateCellGeneration = .off
var hasStrictBoundaries = true
let firstDayOfWeek: DaysOfWeek = .monday
var monthSize: MonthSize? = nil
extension ViewController: JTAppleCalendarViewDelegate, JTAppleCalendarViewDataSource {
func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
formatter.dateFormat = "yyyy-MM-dd"
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.locale = Locale(identifier: "en_US")
let startDate = formatter.date(from: kStartDate)!
let endDate = formatter.date(from: kEndDate)!
let parameters = ConfigurationParameters(startDate: startDate,
endDate: endDate,
numberOfRows: numberOfRows,
calendar: myCalendar,
generateInDates: generateInDates,
generateOutDates: generateOutDates,
firstDayOfWeek: firstDayOfWeek,
hasStrictBoundaries: hasStrictBoundaries)
return parameters
}
func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
let cell = calendar.dequeueReusableCell(withReuseIdentifier: "CellView", for: indexPath) as! CellView
let comedate = String(describing: myCalendar.date(byAdding: .day, value: 1, to: cellState.date))
var freshdate = comedate.substring(from: 9)
freshdate = freshdate.substring(to: 10)
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName:"Mains")
let predicate = NSPredicate (format:"date = %@",freshdate)
fetchRequest.predicate = predicate
if let result = try? context.fetch(fetchRequest) as! [Mains] {
for object in result {
if(object.user! == "" < freshdate) {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "f7bca6")
} else if(object.userme! == "") {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "f7bca6")
} else {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "ffffff")
}
}
}
handleCellConfiguration(cell: cell, cellState: cellState)
return cell
}
}
答案 0 :(得分:2)
我认为它是重用单元格问题,您可以尝试设置默认背景颜色。
if key in dictionary and dictionary[key] != "":
do_something()
答案 1 :(得分:0)
正如@Quoc Le所说,这是一个细胞重用问题。但这里还有另一个问题。
为屏幕上的每个单元格调用此函数。所以简而言之,这就是正在发生的问题。
func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
// Here you got one single cell
let cell = calendar.dequeueReusableCell(withReuseIdentifier: "CellView", for: indexPath) as! CellView
// Here you got a result for your single cell
if let result = try? context.fetch(fetchRequest) as! [Mains] {
// Here you are changing the color of a *single* cell multiple times
// Based on the number of [objects] found in the [result]
// Why?
for object in result {
if(object.user! == "" < freshdate) {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "f7bca6")
} else if(object.userme! == "") {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "f7bca6")
} else {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "ffffff")
}
}
} else {
// There will also be a problem here if you data is not found.
// Your cell background color will just pick up any random re-used color
}
}
您应该做的是准备好DataSource
。
这是一个例子:
let myDataSource: [Mains] = []
override func viewDidLoad() {
super.viewDidLoad()
// Load the data source
myDataSource = try? context.fetch(fetchRequest) as! [Mains]
}
现在,根据数据源中的值,是否要更改单元格的背景颜色?
您似乎正在查看object.user
,object.userme
和empty value
看起来你正在检查任意数据。
cellForItemAt date: Date
函数将单元格映射到日期,而不是任意数据。
因此,您必须将DataSource映射到Date。这可以使用Dictionary
来完成。因此,将DataSource更改为如下所示:
let myDataSource: [String: Mains ] = [:]
// The String can be a date string
// The Mains object can be the object associated with that Date
现在您的cellForItem
功能可能如下所示:
func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
// Here you got one single cell
let cell = calendar.dequeueReusableCell(withReuseIdentifier: "CellView", for: indexPath) as! CellView
// Here you got a result for your single cell
let dateString = dateFormatter.stringFrom(date)
let myObjectForThisCell = myDataSource[dateString]
if(myObjectForThisCell.user! == "" < freshdate) {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "f7bca6")
} else if(myObjectForThisCell.userme! == "") {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "f7bca6")
} else {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "ffffff")
}
}
我上面粘贴的代码不完整。但希望你能够了解如何设置它。您遇到的问题不是日历问题。这是正常UICollectionView
或UITableView
如何运作的问题。
答案 2 :(得分:0)
最后我解决了我刚刚使用的问题;
cell.contentView.backgroundColor = nil
之前;
if let result = try? context.fetch(fetchRequest) as! [Mains] {
for object in result {
if(object.user! == "" < freshdate) {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "f7bca6")
} else if(object.userme! == "") {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "f7bca6")
} else {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "ffffff")
}
}
}
您想要更改颜色,标题等的单元格项目..只需在 = nil 之前添加
工作之后,我认为会帮助很多人。
谢谢。