1

所以我尝试了一下 MonoTouch,我想用自定义对象填充 UITableView,目前是一个简单的矩形。这是绘制表格的代码:

using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;

using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace testProject

{
    [Register ("AppDelegate")]
    public partial class AppDelegate : UIApplicationDelegate
    {
        // class-level declarations
        UIWindow window;
        UITableView table;

    public override bool FinishedLaunching (UIApplication app, NSDictionary options)
    {
        // create a new window instance based on the screen size
        window = new UIWindow (UIScreen.MainScreen.Bounds);

        //make the window white
        var whiteColour = UIColor.FromRGB(255,255,255); 
        window.BackgroundColor = whiteColour;


        //create a UITableView
        table = new UITableView(window.Bounds);
        table.AutoresizingMask = UIViewAutoresizing.All;
        window.Add(table);

                    CellObject newCella = new CellObject();
            UIImage testImage = new UIImage();
            testImage = newCella.DrawCell(window);

            table.CellAt(0).ImageView.Image = testImage;

        // make the window visible
        window.MakeKeyAndVisible ();

        return true;
    }

}
}

还有我正在测试的课程。

using System;
using System.Collections.Generic;
using System.IO;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;

namespace testProject
{
    public class CellObject : UIImage
    {
        //class level vars


            /* notes to self - this class will create an object which will be fed into a uitableview cell.
             * 
             * it will house everything needed for the object view, including gestures etc, it will have the following layers:
             * 1: background layer with social optins, vote, twitter, e-mail to friend etc
             * 2: object image layer with gestures
             * 3: object title layer with animation into view to show more when touched and held
             *
            */

        public UIImage DrawCell (UIWindow window) {
            //create a new graphics context
            int width = (int)Math.Ceiling(window.Bounds.Width);
            int height = (int)Math.Ceiling(window.Bounds.Height);
            CGBitmapContext ctx = new CGBitmapContext(IntPtr.Zero, width, height, 8, 4*width, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst);

            //set up colours etc
            CGColor backgroundColour = new CGColor(1f, 0f, 0f, 1f);

            //draw a rectangle
            ctx.SetFillColor(backgroundColour);
            ctx.FillRect(new RectangleF(width / 2, height / 2, width / 2, height / 2));

            UIImage returnedImage = new UIImage();
            returnedImage = FromImage(ctx.ToImage());
            Console.WriteLine(returnedImage.Size.Width);

            return returnedImage;
        }
    }
}

我没有收到任何错误,一切运行,表格视图显示,但 CellObject 类返回的图像未放入单元格中。我试过把它放到一个新的视图中,同样的事情。检查控制台显示返回的图像是预期的宽度。有任何想法吗?

谢谢。

4

1 回答 1

2

您需要UITableViewSource在显示任何行之前实施,例如此处。像您正在做的手动设置单元格将不起作用。

我建议在进行自定义绘图之前显示一些简单的文本。刚开始cell.TextLabel.Text做一些事情。

于 2013-03-20T17:33:37.137 回答