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 に答える