Reputation: 841
So I'm giving MonoTouch a try, I want to fill a UITableView with custom objects, for the moment a simple rectangle. Here's the code where the table is drawn:
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;
}
}
}
And the class I'm testing with.
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;
}
}
}
I get no errors, everything runs, the table view shows but the CellObject class returned image is not put into the cell. I've tried putting it into a new view, same thing. Checking in the console shows the returned image is the expected width. Any ideas?
Thanks.
Upvotes: 1
Views: 366
Reputation: 26505
You need to be implementing UITableViewSource
before any rows will display, example here. Settings the cells manually like you are doing won't work.
I recommend getting some simple text to be displayed before doing custom drawing. Just set cell.TextLabel.Text
to something for starters.
Upvotes: 2