0

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")
       }
    }
}

一个,但是当我再转一个月时,在日历中看起来一些单元格的背景发生了变化。

下图显示应用打开时为真。

在此处输入图像描述

但是当我过去上个月或下个月改变了下面的一些单元格背景时。这是错误。我不想改变那个。

在此处输入图像描述

我的代码在下面,我会在哪里弄错?

@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
   }
}
4

3 回答 3

2

我认为这是重用单元格的问题,您可以尝试设置默认背景颜色。

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")
}
于 2017-04-27T08:11:06.150 回答
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.usermeempty value

看起来您正在检查任意数据。

cellForItemAt date: 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")
    }
}

我上面粘贴的代码不完整。但希望您了解应该如何设置它。您遇到的问题不是日历问题。这是一个正常UICollectionViewUITableView工作方式的问题。

于 2017-04-27T16:27:48.143 回答
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之前添加

工作后,我想会帮助很多人。

谢谢你。

于 2017-05-03T08:27:46.270 回答