2

我有这个所有者绘制的元素:

using System;
using Tracktion.Service.WCF.Entities;
using MonoTouch.Dialog;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;
using System.Drawing;
using MonoTouch.Foundation;

namespace Tracktion.iOS
{
public class EventListingElement : OwnerDrawnElement
{

    public static UIFont EventTitleFont = UIFont.BoldSystemFontOfSize (18f);
    public static UIFont EventTypeFont = UIFont.SystemFontOfSize (14f);
    public static UIFont CountsFont = UIFont.ItalicSystemFontOfSize (14f);
    public static UIFont DatesFont = UIFont.SystemFontOfSize (14f);
    public static UIColor DefaultBackgroundColor = UIColor.White;

    public EventListingElement (EventListingItem item) : base(UITableViewCellStyle.Default, "EventListingElement")
    {
        ev = item;
    }

    public EventListingElement (EventListingItem item, Action<EventListingItem> tapped) : this(item)
    {
        whenTapped = tapped;
    }

    EventListingItem ev;
    Action<EventListingItem> whenTapped = null;

    public override void Draw (RectangleF bounds, CGContext context, UIView view)
    {
        //view.BackgroundColor = EventListingElement.DefaultBackgroundColor;
        //CGContext currentContext = UIGraphics.GetCurrentContext ();
        SizeF sizeF;
        float num = 0f;
        UIColor.FromRGB (36, 112, 216).SetColor ();
        string dateStr;
        if (ev.StartTime.HasValue)
        {
            TimeSpan t = DateTime.Now - ev.StartTime.Value;
            if (DateTime.Now.Day == ev.StartTime.Value.Day)
            {
                dateStr = ev.StartTime.Value.ToShortTimeString ();
            }
            else
            {
                if (t <= TimeSpan.FromHours (24.0))
                {
                    dateStr = "Yesterday"; //.GetText ();
                }
                else
                {
                    if (t < TimeSpan.FromDays (6.0))
                    {
                        dateStr = ev.StartTime.Value.ToString ("dddd");
                    }
                    else
                    {
                        dateStr = ev.StartTime.Value.ToShortDateString ();
                    }
                }
            }
        }
        else
        {
            dateStr = "Date TBA";
        }
        // Setup counts
        string counts = "Staff & Volunteers: {0} reg, {1} in, {2} out\n";
        counts += "Students & Visitors: {3} reg, {4} in, {5} out";
        counts = string.Format (counts, ev.CountOfStaffAndVolunteersRegistered, ev.CountOfStaffAndVolunteersCheckedIn, ev.CountOfStaffAndVolunteersCheckedOut, ev.CountOfStudentsAndVisitorsRegistered, ev.CountOfStudentsAndVisitorsCheckedIn, ev.CountOfStudentsAndVisitorsCheckedOut);

        float left = 13f;

        // Draw
        sizeF = view.StringSize (dateStr, EventListingElement.EventTitleFont);
        float num2 = sizeF.Width + 21f + 5f;
        RectangleF dateFrame = new RectangleF (view.Bounds.Width - num2, 6f, num2, 14f);
        /*if (ev.StartTime != null && ev.StartTime.Value.Date == DateTime.Today)
        {
            currentContext.SaveState ();
            currentContext.AddEllipseInRect (dateFrame);
            currentContext.Clip ();
            currentContext.DrawLinearGradient (EventListingElement.EventIsTodayGradient, new PointF (10f, 32f), new PointF (22f, 44f), CGGradientDrawingOptions.DrawsAfterEndLocation);
            currentContext.RestoreState ();
        }*/
        view.DrawString (dateStr, dateFrame, EventListingElement.DatesFont, UILineBreakMode.Clip, UITextAlignment.Left);
        float num3 = view.Bounds.Width - left;
        UIColor.Black.SetColor ();
        if (!string.IsNullOrWhiteSpace (ev.EventType))
        {
            view.DrawString (ev.EventType, new PointF (left, 2f), num3 - num2, EventListingElement.EventTypeFont, UILineBreakMode.TailTruncation);
        }
        view.DrawString (ev.Name, new PointF (left, 23f), num3 - left - num, EventListingElement.EventTitleFont, UILineBreakMode.TailTruncation);
        UIColor.Gray.SetColor ();
        view.DrawString (counts, new RectangleF (left, 40f, num3 - num, 34f), EventListingElement.CountsFont, UILineBreakMode.TailTruncation, UITextAlignment.Left);
        /*if (this.NewFlag)
        {
            currentContext.SaveState ();
            currentContext.AddEllipseInRect (new RectangleF (10f, 32f, 12f, 12f));
            currentContext.Clip ();
            currentContext.DrawLinearGradient (MessageSummaryView.gradient, new PointF (10f, 32f), new PointF (22f, 44f), CGGradientDrawingOptions.DrawsAfterEndLocation);
            currentContext.RestoreState ();
        }
        */

        UIColor.Clear.SetFill();
        UIColor.Black.SetColor ();
    }

    public override UITableViewCell GetCell (UITableView tv)
    {
        var cell = base.GetCell (tv);
        cell.BackgroundColor = UIColor.Clear;
        cell.BackgroundView = new UIView(RectangleF.Empty) { BackgroundColor = UIColor.Clear };
        cell.SetNeedsDisplay();
        return cell;
    }

    public override float Height (RectangleF bounds)
    {
        return 90f;
    }

    public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
    {
        base.Selected (dvc, tableView, path);
        if (whenTapped != null)
            whenTapped(this.ev);
    }

}
}

我还没有正确地布置好所有东西,因为我遇到了一个大问题——它最初总是将背景渲染为黑色,没有圆角。但是,在选择单元格并释放选择后,单元格看起来很好,圆角等等。关于我需要做什么的任何建议?我开始使用具有 2 个内部类的 UIView,一个用于单元格,一个用于视图,并且运气相同,并认为切换到 OwnerDrawnElement 可以解决此问题。

4

1 回答 1

1

我删除了 OwnerDrawnElement 作为基类,并简单地扩展了 Element。我发现 Miguel 的一篇旧博客文章对解决这个问题很有帮助。

一些重要的点是扩展 Element 并实现 IElementSizing。GetCell 返回一个 MyDataCell 的实例,该实例具有一个覆盖 draw 方法的自定义 UIView。自定义 UIView 将其 BackgroundColor 设置为清除也非常重要。

public class EventListingElement2 : Element , IElementSizing
{
    EventListingItem _ev;

    public EventListingElement2 (EventListingItem item) : base ("")
    {
        _ev = item;
    }

    public override MonoTouch.UIKit.UITableViewCell GetCell (MonoTouch.UIKit.UITableView tv)
    {
        MyDataCell ret = tv.DequeueReusableCell ("xcx") as MyDataCell;
        if (ret == null) {
            ret = new MyDataCell (_ev, "xcx");
        }
        else {
             ret.UpdateCell (_ev);
        }

        return ret;
    }

    public override void Selected (DialogViewController dvc, UITableView tableView, MonoTouch.Foundation.NSIndexPath path)
    {
        base.Selected (dvc, tableView, path);
    }

    #region IElementSizing implementation
    public float GetHeight (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
    {
        return 90;
    }
    #endregion
}


public class MyDataView : UIView
{
    public MyDataView (EventListingItem myData)
    {
        Update (myData);
        this.BackgroundColor = UIColor.Clear;
    }

    public void Update (EventListingItem myData)
    {
        this._ev = myData;
        SetNeedsDisplay ();
    }

    EventListingItem _ev = new EventListingItem ();

    public override void Draw (RectangleF rect)
    {
        SizeF sizeF;
        float num = 0f;
        UIColor.FromRGB (36, 112, 216).SetColor ();
        string dateStr;
        if (_ev.StartTime.HasValue) {
            TimeSpan t = DateTime.Now - _ev.StartTime.Value;
            if (DateTime.Now.Day == _ev.StartTime.Value.Day) {
                dateStr = _ev.StartTime.Value.ToShortTimeString ();
            } else {
                if (t <= TimeSpan.FromHours (24.0)) {
                    dateStr = "Yesterday"; //.GetText ();
                } else {
                    if (t < TimeSpan.FromDays (6.0)) {
                        dateStr = _ev.StartTime.Value.ToString ("dddd");
                    } else {
                        dateStr = _ev.StartTime.Value.ToShortDateString ();
                    }
                }
            }
        } else {
            dateStr = "Date TBA";
        }
        // Setup counts
        string counts = "Staff & Volunteers: {0} reg, {1} in, {2} out\n";
        counts += "Students & Visitors: {3} reg, {4} in, {5} out";
        counts = string.Format (
            counts,
            _ev.CountOfStaffAndVolunteersRegistered,
            _ev.CountOfStaffAndVolunteersCheckedIn,
            _ev.CountOfStaffAndVolunteersCheckedOut,
            _ev.CountOfStudentsAndVisitorsRegistered,
            _ev.CountOfStudentsAndVisitorsCheckedIn,
            _ev.CountOfStudentsAndVisitorsCheckedOut
        );

        float left = 13f;

        // Draw
        sizeF = this.StringSize (dateStr, EventListingElement.EventTitleFont);
        float num2 = sizeF.Width + 21f + 5f;
        RectangleF dateFrame = new RectangleF (
            this.Bounds.Width - num2,
            6f,
            num2,
            14f
        );

        this.DrawString (
            dateStr,
            dateFrame,
            EventListingElement.DatesFont,
            UILineBreakMode.Clip,
            UITextAlignment.Left
        );
        float num3 = this.Bounds.Width - left;
        UIColor.Black.SetColor ();
        if (!string.IsNullOrWhiteSpace (_ev.EventType)) {
            this.DrawString (
                _ev.EventType,
                new PointF (left, 2f),
                num3 - num2,
                EventListingElement.EventTypeFont,
                UILineBreakMode.TailTruncation
            );
        }
        this.DrawString (
            _ev.Name,
            new PointF (left, 23f),
            num3 - left - num,
            EventListingElement.EventTitleFont,
            UILineBreakMode.TailTruncation
        );
        UIColor.Gray.SetColor ();
        this.DrawString (
            counts,
            new RectangleF (left, 40f, num3 - num, 34f),
            EventListingElement.CountsFont,
            UILineBreakMode.TailTruncation,
            UITextAlignment.Left
        );
    }

}

public class MyDataCell : UITableViewCell
{
    MyDataView _myDataView;

    public MyDataCell (EventListingItem myData, string identKey) : base (UITableViewCellStyle.Default, identKey)
    {
        _myDataView = new MyDataView (myData);
        ContentView.Add (_myDataView);
    }

    public override void LayoutSubviews ()
    {
        base.LayoutSubviews ();
        _myDataView.Frame = ContentView.Bounds;
        _myDataView.SetNeedsDisplay ();
    }

    public void UpdateCell (EventListingItem myData)
    {
        _myDataView.Update (myData);
    }
}
于 2012-05-18T05:28:38.830 回答