如何使用 FSCalendar 为日期显示 2 个不同颜色的点? [英] How do I show 2 different color dots for a date using FSCalendar?

查看:140
本文介绍了如何使用 FSCalendar 为日期显示 2 个不同颜色的点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

初学者程序员在这里.

如果该日期有负交易和正交易,我将尝试显示两个不同颜色的点.

I am trying to show two different color dots if there is a negative AND a positive transaction for that date.

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

Here is my code. It only shows one 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 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
    }

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

This code shows two dots, but they are the same color:

    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
    }

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

How do you tell the FSCalendar delegate methods to show two different color dots on a date?

像这样:

谢谢.

推荐答案

numberOfEventsForDate:(NSDate *)date; 似乎没有正确实现.你返回 1 导致 1 个点

It doesn't appear that numberOfEventsForDate:(NSDate *)date; is correctly implemented. You're returning 1 which results in 1 dot

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

Returning 1 will show 1 dot, 0 will show 0 dots and 2 will display two dots. From the example project

/**
 * 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;

我建议查看 github 存储库中 FSCalendar 的示例.

I would suggest taking a look at the examples on the github repo for FSCalendar.

objc 的实现是这样的

The objc implementation is this

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

如果你总是想要两个点,不管实际的事件数量是多少,只返回 2

If you always want two dots, regardless of the actual number of events, just return 2

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

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

Download the Example.swift project and see the FSCalendarSwiftExample.xcodeproj

这篇关于如何使用 FSCalendar 为日期显示 2 个不同颜色的点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆