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

标签 c# ios uitableview xamarin.ios xamarin

我知道您可以在 iOS Designer 中设置样式,但是如何在代码中做到这一点?

通常您会使用此功能请求单元格

var cell = tableView.DequeueReusableCell (cellIdentifier);

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

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

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

最佳答案

来自Xamarin forum :

if you use RegisterClassForCellReuse, I think you have to use a custom cell that subclasses UITableViewCell. Otherwise it create a standard Cell with style Default.

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

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

并子类化UITableViewCell[Export] 属性是必需的:

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

关于c# - 在 Xamarin iOS 中以编程方式设置 UITableViewCell 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25913332/

相关文章:

javascript - 在移动设备/平板电脑上触摸时,Slick Slider 自动停止播放

带有 2 个输入字段的 iOS 搜索栏(例如要搜索的名称和要搜索的位置的第二行)

iphone - 如何使具有不同 subview 的UITableViewCell可重用?

ios - iOS 13 和 iPhone 11 Pro 模拟器下的 tableView.rowHeight = UITableView.automaticDimension 导致无限更新 subview 循环

objective-c - 从 plist (UITableView) 中删除第一项时应用程序崩溃

swift - 从 Firestore 填充 UITableView

c# - 我可以规定 C# 类型参数只能是接口(interface)类型吗?

c# - Partcover 和 Typemock 集成

C#/.NET 保持命名管道打开并支持任意数量的并行连接客户端

c# - 使用 Prism MVVM 的 OpenFileDialog