Swift 3 中的 JTAppleCalendar 单元格背景颜色更改错误
JTAppleCalendar cell background color change error in Swift 3
我在我的项目中使用 JTAppleCalendar
你可以在这里看到 -> https://github.com/patchthecode/JTAppleCalendar。但是,当我想更改某些单元格中的背景颜色时,我遇到了大问题,当我过去几个月和下个月时,某些单元格背景颜色会发生变化吗?
How it is possible ? How can I fix it ? I want to change only; Example ;
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")
}
}
}
一个,但是当我再过一个月时,在日历中看一些单元格的背景发生了变化。
下图为应用打开时的真实情况。
但是当我在上个月或下个月更改了下面的一些单元格背景时。这是错误的。我不想改变那个。
下面是我的代码,哪里会出错?
@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
}
}
我认为是重用单元格问题,您可以尝试设置默认背景颜色。
if freshdate == "2017-04-16" {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "f7bca6")
} else if freshdate == "2017-04-28" {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "45ca6")
} else {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "your_color")
}
正如@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
函数将单元格映射到 日期 ,而不是任意数据。
因此,您必须将数据源映射到日期。这可以用 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
如何工作的问题。
最后我解决了我使用的问题;
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
之前添加
工作后,我想会帮助到很多人。
谢谢。
我在我的项目中使用 JTAppleCalendar
你可以在这里看到 -> https://github.com/patchthecode/JTAppleCalendar。但是,当我想更改某些单元格中的背景颜色时,我遇到了大问题,当我过去几个月和下个月时,某些单元格背景颜色会发生变化吗?
How it is possible ? How can I fix it ? I want to change only; Example ;
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")
}
}
}
一个,但是当我再过一个月时,在日历中看一些单元格的背景发生了变化。
下图为应用打开时的真实情况。
但是当我在上个月或下个月更改了下面的一些单元格背景时。这是错误的。我不想改变那个。
下面是我的代码,哪里会出错?
@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
}
}
我认为是重用单元格问题,您可以尝试设置默认背景颜色。
if freshdate == "2017-04-16" {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "f7bca6")
} else if freshdate == "2017-04-28" {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "45ca6")
} else {
cell.contentView.backgroundColor = hexStringToUIColor(hex: "your_color")
}
正如@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
函数将单元格映射到 日期 ,而不是任意数据。
因此,您必须将数据源映射到日期。这可以用 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
如何工作的问题。
最后我解决了我使用的问题;
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
之前添加工作后,我想会帮助到很多人。
谢谢。