我有一个带有自定义单元格的UITableView
。该TableView位于 View Controller 中,并占据 View 的完整尺寸。每个单元格的 RowHeight
设置为 TableView 的高度,因此每个单元格占据整个屏幕(减去导航栏)。这是该设置的屏幕截图。 Red
是我的自定义单元格;向下滑动将翻到下一页,该页具有不同的颜色:
当 TableView 首次在屏幕上可见时,我将模型(颜色集合)传递给 Setup
方法,然后该方法设置 Delegates
。在此初始设置期间,UITableViewDataSource
委托(delegate)的 GetCell
方法会为每个单元格调用,尽管一次只有一个单元格可见。然后,这会触发 UITableViewDelegate
类的 WillDisplay
和 CellDisplayingEnded
委托(delegate)方法。
我的印象是,UITableView
只会根据需要创建每个单元格的实例(当变得可见时),但它似乎正在为加载时的每个单元格创建一个实例,无论可见性如何.
我创建了一个精简的示例来共享,该表总共有 6 条记录。然后,我在上述每个委托(delegate)方法的开头添加了一条日志,这是设置 TableView 时的输出:
[0:] GetCell called. Row 0
[0:] WillDisplayCell called. Row 0, RowHeight 672
[0:] GetCell called. Row 1
[0:] WillDisplayCell called. Row 1, RowHeight 672
[0:] GetCell called. Row 2
[0:] WillDisplayCell called. Row 2, RowHeight 672
[0:] GetCell called. Row 3
[0:] WillDisplayCell called. Row 3, RowHeight 672
[0:] GetCell called. Row 4
[0:] WillDisplayCell called. Row 4, RowHeight 672
[0:] GetCell called. Row 5
[0:] WillDisplayCell called. Row 5, RowHeight 672
[0:] CellDisplayingEnded called. Row 1, RowHeight: 672
[0:] CellDisplayingEnded called. Row 2, RowHeight: 672
[0:] CellDisplayingEnded called. Row 3, RowHeight: 672
[0:] CellDisplayingEnded called. Row 4, RowHeight: 672
[0:] CellDisplayingEnded called. Row 5, RowHeight: 672
如前所述,屏幕上一次只能看到一个单元格。为什么最初创建了所有 6 个单元?
示例代码
public class SampleCell : UITableViewCell {
public void Setup(UIColor color) {
BackgroundColor = color;
}
}
public class SampleTableViewDataSourceDelegate : UITableViewDataSource {
public SampleTableViewDataSourceDelegate(UIColor[] model) {
this.model = model;
}
private readonly UIColor[] model;
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) {
Debug.WriteLine($"GetCell called. Row {indexPath.Row}");
var reuseIdentifier = "SampleCell";
var cell = tableView.DequeueReusableCell(reuseIdentifier) as SampleCell;
cell = (cell ?? (cell = new SampleCell()));
cell.Setup(model[indexPath.Row]);
return cell;
}
public override nint NumberOfSections(UITableView tableView) {
return 1;
}
public override nint RowsInSection(UITableView tableView, nint section) {
return model.Length;
}
}
public class SampleTableViewDelegate : UITableViewDelegate {
public SampleTableViewDelegate(UIColor[] model) {
this.model = model;
}
private readonly UIColor[] model;
public override void WillDisplay(UITableView tableView, UITableViewCell cell, NSIndexPath indexPath) {
Debug.WriteLine($"WillDisplayCell called. Row {indexPath.Row}, RowHeight {tableView.RowHeight}");
}
public override void CellDisplayingEnded(UITableView tableView, UITableViewCell cell, NSIndexPath indexPath) {
Debug.WriteLine($"CellDisplayingEnded called. Row {indexPath.Row}, RowHeight: {tableView.RowHeight}");
}
}
public partial class SampleTableView : TableViewBase {
public SampleTableView(IntPtr handle) : base(handle) {
PagingEnabled = true;
}
private UIColor[] model;
public void Setup(UIColor[] model) {
this.model = model;
Delegate = new SampleTableViewDelegate(model);
DataSource = new SampleTableViewDataSourceDelegate(model);
}
}
然后,在我的 ViewController
中,我按如下方式传递模型:
public override void ViewDidLoad() {
base.ViewDidLoad();
var model = new UIColor[] {
UIColor.Green,
UIColor.Red,
UIColor.Magenta,
UIColor.Cyan,
UIColor.Blue,
UIColor.Purple
};
SampleTableView.RowHeight = SampleTableView.Frame.Height;
SampleTableView.Setup(model);
}
最佳答案
噢,法律,还需要设置EstimatedRowHeight
。
SampleTableView.EstimatedRowHeight = SampleTableView.Frame.Height;
这解决了我的问题。现在,GetCell
仅在加载时调用一次,因为一次只有一个可见单元格。
文档(苹果,但 xamarin 也一样): https://developer.apple.com/documentation/uikit/uitableview/1614925-estimatedrowheight
关于c# - Xamarin iOS UITableView 在初始设置时调用所有单元格的委托(delegate)方法,而不仅仅是可见单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54808375/