c# - Xamarin iOS UITableView 在初始设置时调用所有单元格的委托(delegate)方法,而不仅仅是可见单元格

标签 c# ios uitableview xamarin xamarin.ios

我有一个带有自定义单元格的UITableView。该TableView位于 View Controller 中,并占据 View 的完整尺寸。每个单元格的 RowHeight 设置为 TableView 的高度,因此每个单元格占据整个屏幕(减去导航栏)。这是该设置的屏幕截图。 Red 是我的自定义单元格;向下滑动将翻到下一页,该页具有不同的颜色:

UITableView Cell

当 TableView 首次在屏幕上可见时,我将模型(颜色集合)传递给 Setup 方法,然后该方法设置 Delegates。在此初始设置期间,UITableViewDataSource 委托(delegate)的 GetCell 方法会为每个单元格调用,尽管一次只有一个单元格可见。然后,这会触发 UITableViewDelegate 类的 WillDisplayCellDisplayingEnded 委托(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/

相关文章:

ios - 使用 NSTimer 从 0.00 计数到 176.20

ios - 如何检查 iOS 7 中特定应用程序是否启用了位置服务?

ios - 用 librtmp 编译 ffmpeg

ios - indexpath.row 从 1 而不是 0 开始

ios - 如何禁用特定 UITableView 部分的用户交互

iphone - 在 UITableView 中获取部分的标题

c# - 两组点之间的最佳匹配

c# - 如何从另一个类库项目中的 app.config 获取连接字符串?

c# - 异步 TCP 操作期间的 AccessViolation

c# - TryParse 循环中的两个变量