I need to change the cell background color when a user taps a cell in the month view. In MonthViewDayTapped, I'm currently returning false, to effectively disable the day view, since we don't need that. If there's no property/method, is there any trick I can use to set the cell color? Ideally, I'd be able to get to each of the UIView objects for each cell in the calendar somehow, and have full access, since not everybody is going to use a calendar to show appointments (show images, etc.), but the color would be nice to set in particular. Thanks!
Hi Michael,
Thats a great feature request, and one i think we can add easily enough.
It'd be really helpful if you aded it on our "ideas" page (which i believe you're familiar with)
http://ideas.infragistics.com/forums/211525-nuclios
We're always working to get those suggestions into our controls, for example the feature request, which you've commented on: IGCalendarView Appointment Colors is already implemented and was put into the last SR.
In the mean time, there is an unconventional way to get at the view you'd like to manipulate:
-(BOOL)calendarView:(IGCalendarView *)calView monthViewDayTapped:(NSDate *)date
{
_prevSelectedDayView.backgroundColor = [UIColor clearColor];
UIView* multiMonthView = [_cal valueForKey:@"_monthView"];
Class c = NSClassFromString(@"DateInfo");
id dateInfo = objc_msgSend(c, @selector(resolveInfoForDate:usingCalendarView:), date, calView);
UIView* monthView = objc_msgSend(multiMonthView, @selector(resolveMonthViewForDate:), dateInfo);
UIView* monthViewDay = objc_msgSend(monthView, @selector(resolveMonthViewDayForDate:), dateInfo);
monthViewDay.backgroundColor = [UIColor orangeColor];
_prevSelectedDayView = monthViewDay;
return NO;
}
Hope this helps,
-SteveZ
Steve,
I'm finally getting to try this out. If you have time, could you take a stab at converting your sample code above to C#? I haven't used the msgSend from Xamarin, so trying to get it working. Thanks!
Michael
Sure! I meant to do that originally, but when from your last reply, I just assumed you got it working:
using MonoTouch.ObjCRuntime;
public class CalViewDelegate :IGCalendarViewDelegate { UIView _prevSelectedDayView; public override bool MonthViewDayTapped (IGCalendarView calView, NSDate date) { if (_prevSelectedDayView != null) _prevSelectedDayView.BackgroundColor = UIColor.Clear; UIView multiMonthView = (UIView)calView.ValueForKey (new NSString("_monthView")); IntPtr dateInfoPtr = Messaging.IntPtr_objc_msgSend_IntPtr_IntPtr ((new Class ("DateInfo")).Handle, (new Selector (new NSString ("resolveInfoForDate:usingCalendarView:"))).Handle, date.Handle, calView.Handle); IntPtr monthViewPtr = Messaging.IntPtr_objc_msgSend_IntPtr (multiMonthView.Handle, new Selector ("resolveMonthViewForDate:").Handle, dateInfoPtr); IntPtr monthViewDayPtr = Messaging.IntPtr_objc_msgSend_IntPtr (monthViewPtr, new Selector ("resolveMonthViewDayForDate:").Handle, dateInfoPtr); _prevSelectedDayView = new UIView (monthViewDayPtr); _prevSelectedDayView.BackgroundColor = UIColor.Orange; return false; } }
We use this code (the xamarin version) in our application, however there seems to be a defect. Occasionally, if you tap several cells the colour seems to "stick" so we end up with two or more highlighted cells. Could you let me know if there's a resolution for this, or if it's a known defect and when there will be a fix available?Thanks
Hi Paul,
I was playing around with the code snippet i posted, and its not reproducing on my end.
As long as you're not losing _prevSelectedDayView then you'll always be able reset it. Can you validate that _prevSelectedDayView is not getting set to null somewhere? If you were to lose that value, then i could see it sticking, but that would be the only case. That view object is only created once when the control is created, so it wouldn't be getting lost on the control side.
Ok, i figured it out. Looks like Xamarin can occasionally lose the connection with a view thats created with a IntPtr.
So instead, we should just store off the ptr:
IntPtr _prevSelectedDayViewPtr;
if (_prevSelectedDayViewPtr != null) { UIView prevDayView = new UIView (_prevSelectedDayViewPtr); prevDayView.BackgroundColor = UIColor.Clear; } UIView multiMonthView = (UIView)calView.ValueForKey (new NSString("_monthView")); IntPtr dateInfoPtr = Messaging.IntPtr_objc_msgSend_IntPtr_IntPtr ((new Class ("DateInfo")).Handle, (new Selector (new NSString ("resolveInfoForDate:usingCalendarView:"))).Handle, date.Handle, calView.Handle); IntPtr monthViewPtr = Messaging.IntPtr_objc_msgSend_IntPtr (multiMonthView.Handle, new Selector ("resolveMonthViewForDate:").Handle, dateInfoPtr); IntPtr monthViewDayPtr = Messaging.IntPtr_objc_msgSend_IntPtr (monthViewPtr, new Selector ("resolveMonthViewDayForDate:").Handle, dateInfoPtr); _prevSelectedDayViewPtr = monthViewDayPtr; UIView day = new UIView (monthViewDayPtr); day.BackgroundColor = UIColor.Orange;
Hey Paul,
I went back and played around with it again, and I was able to reproduce it.
I'll try and figure out whats going on and get back to you.