我是C#的新手,我遇到的问题是自定义单元格无法在IOS上正确呈现。我为Android和IOS创建了一个自定义渲染器。 android渲染器正确调整了单元格的大小,但是在IOS上,单元格出现时单元格上的所有字段都是抽筋,但单元格没有调整大小以适应所有字段。如何强制IOS上的单元格调整大小并容纳服装单元格的所有字段?以下是我的代码:
通用细胞代码
public class NativeCell : ViewCell
{
public static readonly BindableProperty HostNameProperty =
BindableProperty.Create ("HostName", typeof(string), typeof(NativeCell), "");
public string HostName {
get { return "HostName: " + (string)GetValue (HostNameProperty); }
set { SetValue (HostNameProperty, value); }
}
public static readonly BindableProperty OSNameProperty =
BindableProperty.Create ("OSName", typeof(string), typeof(NativeCell), "");
public string OSName
{
get { return "OS Name: " + (string)GetValue (OSNameProperty); }
set { SetValue (OSNameProperty, value); }
}
public static readonly BindableProperty IPAddressProperty =
BindableProperty.Create("IPAddress", typeof(string), typeof(NativeCell), "");
public string IPAddress
{
get { return "IP Address: " + (string)GetValue(IPAddressProperty); }
set { SetValue(IPAddressProperty, value); }
}
public static readonly BindableProperty OnlineSinceProperty =
BindableProperty.Create("OnlineSince", typeof(string), typeof(NativeCell), "");
public string OnlineSince
{
get { return "Online Since: " + (string)GetValue(OnlineSinceProperty); }
set { SetValue(OnlineSinceProperty, value); }
}
public static readonly BindableProperty ImageFilenameProperty =
BindableProperty.Create ("ImageFilename", typeof(string), typeof(NativeCell), "");
public string ImageFilename {
get { return (string)GetValue (ImageFilenameProperty); }
set { SetValue (ImageFilenameProperty, value); }
}
}
IOS上的渲染器和客户Cell
public class NativeiOSCell : UITableViewCell
{
UILabel hostName,osVersion, ipAdddress, onlineSince;
UIImageView imageView;
public NativeiOSCell (NSString cellId) : base (UITableViewCellStyle.Default, cellId)
{
SelectionStyle = UITableViewCellSelectionStyle.Gray;
ContentView.BackgroundColor = UIColor.FromRGB (255, 255, 224);
imageView = new UIImageView ();
hostName = new UILabel () {
Font = UIFont.FromName ("Cochin-BoldItalic", 15f),
TextColor = UIColor.FromRGB (127, 51, 0),
BackgroundColor = UIColor.Clear
};
osVersion = new UILabel()
{
Font = UIFont.FromName("Cochin-BoldItalic", 15f),
TextColor = UIColor.FromRGB(127, 51, 0),
BackgroundColor = UIColor.Clear
};
ipAdddress = new UILabel()
{
Font = UIFont.FromName("Cochin-BoldItalic", 15f),
TextColor = UIColor.FromRGB(127, 51, 0),
BackgroundColor = UIColor.Clear
};
onlineSince = new UILabel()
{
Font = UIFont.FromName("Cochin-BoldItalic", 15f),
TextColor = UIColor.FromRGB(127, 51, 0),
BackgroundColor = UIColor.Clear
};
ContentView.Add(hostName);
ContentView.Add(osVersion);
ContentView.Add(ipAdddress);
ContentView.Add(onlineSince);
ContentView.Add (imageView);
}
public void UpdateCell (string name, string os, string ip, string online, UIImage image)
{
hostName.Text = name;
osVersion.Text = os;
ipAdddress.Text = ip;
onlineSince.Text = online;
imageView.Image = image;
}
public override void LayoutSubviews ()
{
base.LayoutSubviews ();
//osVersion.Frame = new CoreGraphics.CGRect (5, 55, ContentView.Bounds.Width - 63, 25);
hostName.Frame = new CoreGraphics.CGRect (100, 5, 250, 25);
osVersion.Frame = new CoreGraphics.CGRect (100, 100, 250, 25);
ipAdddress.Frame = new CoreGraphics.CGRect(100, 200, 250, 25);
onlineSince.Frame = new CoreGraphics.CGRect(100, 300, 250, 25);
imageView.Frame = new CoreGraphics.CGRect (5, 5, 50, 50);
}
}
public class NativeiOSCellRenderer : ViewCellRenderer
{
static NSString rid = new NSString ("NativeCell");
public override UITableViewCell GetCell (Xamarin.Forms.Cell item, UITableViewCell reusableCell, UITableView tv)
{
var x = (NativeCell)item;
Console.WriteLine (x);
NativeiOSCell c = reusableCell as NativeiOSCell;
if (c == null) {
c = new NativeiOSCell (rid);
}
UIImage i = null;
if (!String.IsNullOrWhiteSpace (x.ImageFilename)) {
i = UIImage.FromFile ("Images/" + x.ImageFilename + ".jpg");
}
c.UpdateCell (x.HostName, x.OSName, x.IPAddress, x.OnlineSince, i);
return c;
}
}