How do I show 2 different color dots for a date using FSCalendar?

南笙酒味 提交于 2021-02-10 23:41:46

问题


Beginner programmer here.

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
    }

single dot

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
    }

two dots

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

Like This:

two different dots

Thanks.


回答1:


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

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;

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

The objc implementation is this

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

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
}

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



来源:https://stackoverflow.com/questions/63039916/how-do-i-show-2-different-color-dots-for-a-date-using-fscalendar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!