gpt4 book ai didi

ios - 当内容高于 EstimatedSize (sizeHint) 时,为什么单元格不调整大小

转载 作者:行者123 更新时间:2023-11-29 13:54:34 27 4
gpt4 key购买 nike

我从 Swift 移植了一个带有可变高度单元格的 TableView 实现。但在我的 Xamarin/ReactiveUI 实现中,单元格不会在内容增长时调整大小(即显示可选标签时)。请注意,单元格调整大小以适应 native Swift 实现。

大多数在线答案都集中在 UITableView 上的两个属性的设置上: RowHeightEstimatedHeight以及使用自动布局。我的行高设置为 UITableView.AutomaticDimensionEstimatedHeight设置为 44f .我正在使用我将在下面展示的自动布局约束。我正在使用 BindTo ReactiveTableViewSourceExtensions 中的扩展.

我还尝试设置 Text立即检查可选标签的属性,以检验预先设置该属性会引起调整大小的理论。

以下是相关的代码行:在 ReactiveViewController<T>负责人:

this.WhenActivated(disposables =>
{
//// ...

// This method automatically wires up the cell reuse key to be the nameof my cell class which is what I want.
this.WhenAnyValue(view => view._cells)
.BindTo<IImportedFileViewModel, ImportedFileCell>(TableView, 44f)
.DisposeWith(disposables);
//// ...
});

ViewDidLoad 下方:

//// ...

TableView = new UITableView
{
RowHeight = UITableView.AutomaticDimension,
EstimatedRowHeight = 44f, // Setting or not setting this doesn't matter
SeparatorStyle = UITableViewCellSeparatorStyle.None,
AllowsSelection = false,
TranslatesAutoresizingMaskIntoConstraints = false,
};

//// ...

在单元实现中:

ClipsToBounds = true;
PreservesSuperviewLayoutMargins = true;
ContentView.ClipsToBounds = true;
ContentView.PreservesSuperviewLayoutMargins = true;

var stackView = new UIStackView
{
TranslatesAutoresizingMaskIntoConstraints = false,
Axis = UILayoutConstraintAxis.Vertical,
Distribution = UIStackViewDistribution.Fill,
Alignment = UIStackViewAlignment.Fill,
Spacing = 4,
};

var fileStackView = new UIStackView
{
TranslatesAutoresizingMaskIntoConstraints = false,
Axis = UILayoutConstraintAxis.Horizontal,
Distribution = UIStackViewDistribution.Fill,
Alignment = UIStackViewAlignment.Top,
};

FilenameLabel = new UILabel
{
TranslatesAutoresizingMaskIntoConstraints = false,
Font = UIFont.SystemFontOfSize(15f, UIFontWeight.Medium),
};

fileStackView.AddArrangedSubview(FilenameLabel);

StatusImage = new UIImageView()
{
TranslatesAutoresizingMaskIntoConstraints = false,
ContentMode = UIViewContentMode.Center,
};

fileStackView.AddArrangedSubview(StatusImage);
stackView.AddArrangedSubview(fileStackView);

var reasonStackView = new UIStackView
{
TranslatesAutoresizingMaskIntoConstraints = false,
Axis = UILayoutConstraintAxis.Horizontal,
Distribution = UIStackViewDistribution.Fill,
Alignment = UIStackViewAlignment.Top,
};

// This is the optional label that, when its Text property is set, should resize the cell.
FailureReasonLabel = new UILabel
{
TranslatesAutoresizingMaskIntoConstraints = false,
Font = UIFont.SystemFontOfSize(13f, UIFontWeight.Medium),
Lines = 0,
};

reasonStackView.AddArrangedSubview(FailureReasonLabel);
stackView.AddArrangedSubview(reasonStackView);
ContentView.AddSubview(stackView);

stackView.BottomAnchor.ConstraintEqualTo(ContentView.LayoutMarginsGuide.BottomAnchor).Active = true;
stackView.TopAnchor.ConstraintEqualTo(ContentView.LayoutMarginsGuide.TopAnchor).Active = true;
stackView.LeadingAnchor.ConstraintEqualTo(ContentView.LayoutMarginsGuide.LeadingAnchor).Active = true;
stackView.TrailingAnchor.ConstraintEqualTo(ContentView.LayoutMarginsGuide.TrailingAnchor).Active = true;

StatusImage.HeightAnchor.ConstraintEqualTo(16f).Active = true;
StatusImage.WidthAnchor.ConstraintEqualTo(16f).Active = true;

我很乐意使用我现在拥有的基本结构来解决问题但是我也愿意接受一些其他模式的例子,有人已经使用(基于 ReactiveUI)来获得这个工作。我希望该解决方案不基于旧的手动调整大小模式,例如 iOS 8 天前的版本或某种黑客攻击。

最佳答案

所以必须调整两件事来解决我的问题。

  1. ReactiveUI BindTo 方法中的 sizeHint 参数的行为与 TableView.EstimatedRowHeight 不同,这是我假设的。所以我最终将它设置为自动尺寸常量,如下所示:
.BindTo<IImportedFileViewModel, ImportedFileCell>(TableView, (float)UITableView.AutomaticDimension)
  1. 我想我可以在传递给 BindTo 方法的 IObservableCollection 实现中更新 View 模型的属性,但直到我集合本身( View 绑定(bind)到的集合)开始激发更改事件,单元格开始调整自身大小。因此,由于我将 DynamicData 用作 ReactiveUI 的一部分,这意味着每当我知道 FailureReasonLabel 已设置时调用 SourceCache.AddOrUpdate(updatedViewModel)

我还将尝试恢复从这篇文章中删除的 ReactiveUI 标签,因为我相信它是这个问题及其答案的相关部分。

关于ios - 当内容高于 EstimatedSize (sizeHint) 时,为什么单元格不调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57348005/

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