gpt4 book ai didi

c# - 如何将自定义 `UITableCell` 提供给 `UITableView`,使其具有与内置单元格相同的布局指标?

转载 作者:行者123 更新时间:2023-11-28 23:57:53 25 4
gpt4 key购买 nike

当我将 subview 添加到原型(prototype)表格单元格时,比方说 UITextView,默认情况下没有左边距。我希望它使用与 iOS 内部用于内置标准单元格布局相同的边距。如果我遗漏了明显的东西,“使用设备/iOS 默认值”标志在哪里?否则,我的首选解决方案是查询设备的设备相关和 iOS 版本相关 UI 指标的方法。出于此问题的目的,我们可以将其限制为仅 UITableView 控件及其 UITableCell 后代的指标。

这就是我生成自定义单元格的方式:

    internal class UITextSingleline : UITableViewCell
{
UILabel headingLabel;
UITextView textBox;
int MultiHeight;
public bool secure;

public UITextSingleline() : this(null, 1) { }
public UITextSingleline(int multiheight) : this(null, multiheight) { }
public UITextSingleline(NSString cellId, int multiheight) : base(UITableViewCellStyle.Default, cellId)
{
MultiHeight = multiheight;
SelectionStyle = UITableViewCellSelectionStyle.None;
headingLabel = new UILabel()
{
Font = UIFont.SystemFontOfSize(16),
TextColor = UIColor.DarkTextColor,
BackgroundColor = UIColor.Clear
};

textBox = new UITextView()
{
ClipsToBounds = true,
Font = UIFont.SystemFontOfSize(16),
TextColor = UIColor.DarkTextColor
};

if (multiheight == 1) textBox.TextContainer.MaximumNumberOfLines = 1;
textBox.Layer.CornerRadius = 10.0f;
textBox.Layer.BorderColor = UIColor.DarkTextColor.CGColor;
textBox.Layer.BorderWidth = 1f;
ContentView.AddSubviews(new UIView[] { headingLabel, textBox });
}

public override void LayoutSubviews()
{
base.LayoutSubviews();
headingLabel.Frame = new CGRect(16, 8, ContentView.Bounds.Width - 32, 20);
textBox.Frame = new CGRect(16, 32, ContentView.Bounds.Width - 32, 36 * MultiHeight); /* see? magic numbers all over the place */
}

public void UpdateCell(string caption, string text)
{
headingLabel.Text = caption;
textBox.Text = text;
}

public string Text
{
get
{
return textBox?.Text;
}
set
{
if (textBox != null) textBox.Text = value;
}
}
}

这就是它链接到包含 TableView 的方式:

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
switch (indexPath.Section)
{
case 0:
/* area/workplace */
switch (indexPath.Row)
{
case 0:
/* area picker (omitted) */
case 1:
/* workplace text input (single-line) */
if (txtWorkplace == null)
{
txtWorkplace = new UITextSingleline();
txtWorkplace.UpdateCell("Workplace", Data.Instance.Payload.report.workplace);
}
return txtWorkplace;
}
break;
case 1:
/* rest ommitted for brevity */
break;
}
return null;
}

我在 SO 和互联网上搜索了与 systemmetrics 等价的东西,有很多颜色和字体,但我只发现了很少的关于尺寸、边距、插图、圆角半径等的信息:

  • > This question处理相反的事情,去掉任何边距,并且没有提到如何复制 iOS 默认值。
  • > This page from apple dev提到整个 TableViewseparatorInset 属性,它似乎适用于左缩进,但我对将布局的一部分应用到另一部分的度量有点怀疑部分。

硬编码魔数(Magic Number)不是一种选择。我们在 iPhone 和 iPad 上进行了测试,发现即使在同一台设备上也有不同的 inset 默认值,只是 iOS 版本不同。我也会对 Objective-C 和 Swift 提示和解决方案感到满意,只要它们在正确翻译后在 xamarin 中工作即可。

最佳答案

如果你想根据不同的设备或不同的版本使用默认边距,为什么不试试自动布局呢? NSLayoutAttribute.LeadingMargin 表示元素边距的默认前沿。在您的 UITextSingleline 中,将 LayoutSubviews() 从硬代码修改为自动布局:

假设单元格只有一个标签来显示一些文本:

public override void LayoutSubviews()
{
base.LayoutSubviews();

var leadingConstraint = NSLayoutConstraint.Create(headingLabel, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.LeadingMargin, 1.0f, 0);
var topConstraint = NSLayoutConstraint.Create(headingLabel, NSLayoutAttribute.Top, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.TopMargin, 1.0f, 0);
var trailingConstraint = NSLayoutConstraint.Create(headingLabel, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.TrailingMargin, 1.0f, 0);
var bottomConstraint = NSLayoutConstraint.Create(headingLabel, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.BottomMargin, 1.0f, 0);

ContentView.AddConstraints(new NSLayoutConstraint[] { leadingConstraint, topConstraint, trailingConstraint, bottomConstraint });
}

这样,headingLabel 将具有与“标准内置单元格的 TextLabel”相同的布局。

而且在你的情况下,你似乎也想在你的单元格中添加一个 UITextView 。我建议你在构造函数时添加约束,我为你提供了我的约束,引用:

public MyTableViewCell (IntPtr handle) : base (handle)
{
headingLabel = new UILabel()
{
Font = UIFont.SystemFontOfSize(17),
TextColor = UIColor.DarkTextColor,
BackgroundColor = UIColor.Clear,
Lines = 0
};

textBox = new UITextView()
{
ClipsToBounds = true,
Font = UIFont.SystemFontOfSize(16),
TextColor = UIColor.DarkTextColor
};

ContentView.AddSubview(headingLabel);
ContentView.AddSubview(textBox);

// Disable this to enable autolayout
headingLabel.TranslatesAutoresizingMaskIntoConstraints = false;
textBox.TranslatesAutoresizingMaskIntoConstraints = false;

doLayouts();
}

void doLayouts()
{

var leadingConstraint = NSLayoutConstraint.Create(headingLabel, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.LeadingMargin, 1.0f, 0);
var topConstraint = NSLayoutConstraint.Create(headingLabel, NSLayoutAttribute.Top, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.TopMargin, 1.0f, 0);
var trailingConstraint = NSLayoutConstraint.Create(headingLabel, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.TrailingMargin, 1.0f, 0);

ContentView.AddConstraints(new NSLayoutConstraint[] { leadingConstraint, topConstraint, trailingConstraint });

var boxLeading = NSLayoutConstraint.Create(textBox, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.LeadingMargin, 1.0f, 0);
var boxTop = NSLayoutConstraint.Create(textBox, NSLayoutAttribute.Top, NSLayoutRelation.Equal, headingLabel, NSLayoutAttribute.Bottom, 1.0f, 4);
var boxTrailing = NSLayoutConstraint.Create(textBox, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.TrailingMargin, 1.0f, 0);
var boxBottom = NSLayoutConstraint.Create(textBox, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.BottomMargin, 1.0f, 0);
var boxHeight = NSLayoutConstraint.Create(textBox, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1.0f, 36 * MultiHeight);

ContentView.AddConstraints(new NSLayoutConstraint[] { boxLeading, boxTop, boxTrailing, boxBottom, boxHeight });
}

使用 AutoLayout 的另一个好处是:在将 TableView 的 RowHeight 设置为 UITableView.AutomaticDimension 和 EstimatedHeight 之后,如果我们设置了正确的约束,单元格将根据其内容自动计算行高。

关于c# - 如何将自定义 `UITableCell` 提供给 `UITableView`,使其具有与内置单元格相同的布局指标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50602592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com