在Xamarin iOS中以编程方式设置UITableViewCell样式

本文关键字:方式 设置 UITableViewCell 样式 编程 Xamarin iOS | 更新日期: 2023-09-27 18:22:17

我知道你可以在iOS Designer中设置样式,但在代码中如何设置?

通常情况下,您请求具有此功能的单元格

var cell = tableView.DequeueReusableCell (cellIdentifier);

我看到了Objective-C解决方案,它创建了一个新的单元。在新的paradigma之后,如果您使用RegisterClassForCellReuse,则不需要创建新的单元格。

这是在代码中设置样式的唯一方法吗?

if (cell == null) {
    cell = new UITableViewCell (UITableViewCellStyle.Subtitle, cellIdentifier);
}

在Xamarin iOS中以编程方式设置UITableViewCell样式

来自Xamarin论坛:

如果您使用RegisterClassForCellReuse,我认为您必须使用一个自定义单元格,它是UITableViewCell的子类。否则,它将创建样式为Default的标准单元。

以下通过在ViewDidLoad中注册类来工作:

TableView.RegisterClassForCellReuse(typeof(SubtitleTableViewCell), new NSString("SessionCell"));

并将CCD_ 4。[Export]属性是必需的:

public class SubtitleTableViewCell : UITableViewCell
{
    [Export("initWithStyle:reuseIdentifier:")]
    public SubtitleTableViewCell(UITableViewCellStyle style, string cellId)
        : base( UITableViewCellStyle.Subtitle, cellId) { }
}