如何使用FSCalendar为日期显示2个不同的色点?

时间:2020-07-22 17:26:47

标签: swift realm nspredicate fscalendar

这里是初学者。

如果该日期的交易额为负数,则我尝试显示两个不同的色点。

这是我的代码。它只显示一个点。

func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
        
        let incomeTransaction = realm.objects(Transaction.self).filter(positiveTransactionPredicate(date: date))
        
        let expenseTransaction = realm.objects(Transaction.self).filter(negativeTransactionPredicate(date: date))
        
        for _ in incomeTransaction {
            return 1
        }
        for _ in expenseTransaction {
            return 1
        }
        return 0
    }

func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, eventDefaultColorsFor date: Date) -> [UIColor]? {
        
        let incomeTransaction = realm.objects(Transaction.self).filter(positiveTransactionPredicate(date: date))
        
        let expenseTransaction = realm.objects(Transaction.self).filter(negativeTransactionPredicate(date: date))
        
        for _ in incomeTransaction {
            return [UIColor(rgb: Constants.green)]
        }
        for _ in expenseTransaction {
            return [UIColor(rgb: Constants.red)]
        }
        return nil
    }

single dot

此代码显示两个点,但是它们具有相同的颜色:

    func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
        
        let incomeTransaction = realm.objects(Transaction.self).filter(positiveTransactionPredicate(date: date))
        
        let expenseTransaction = realm.objects(Transaction.self).filter(negativeTransactionPredicate(date: date))
            
        for _ in incomeTransaction {
            return 2
        }
        for _ in expenseTransaction {
            return 2
        }
        
        return 0
    }

two dots

您如何告诉FSCalendar委托方法在日期上显示两个不同的色点?

赞!

two different dots

谢谢。

1 个答案:

答案 0 :(得分:0)

似乎没有正确实现numberOfEventsForDate:(NSDate *)date;。您返回1,结果为1点

返回1将显示1个点,返回0将显示0个点,返回2将显示两个点。来自示例项目

/**
 * Asks the dataSource the number of event dots for a specific date.
 *
 * @see
 *   - (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance eventColorForDate:(NSDate *)date;
 *   - (NSArray *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance eventColorsForDate:(NSDate *)date;
 */
- (NSInteger)calendar:(FSCalendar *)calendar numberOfEventsForDate:(NSDate *)date;

我建议看一下FSCalendar的github仓库中的示例。

objc实现是这个

//return 2 for 2 events
cell.numberOfEvents = [self.dataSourceProxy calendar:self numberOfEventsForDate:date]; 

如果您始终想要两个点,而不考虑实际的事件数,只需返回2

func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
   return 2
}

下载Example.swift项目并查看FSCalendarSwiftExample.xcodeproj