我知道您可以在 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/