在 Xamarin.iOS 中使用不同的单元格样式
Use different cell style in Xamarin.iOS
我想创建一个简单的示例,我想在其中使用另一个 UITableViewCellStyle
。在我的示例中,它是 UITableViewCellStyleValue1
。我在故事板中添加了一个 UITableViewController
,将 class 名称设置为 NakedTableViewController,从而使用 Dynamic Prototypes as Content,然后我将 Table View Cell Style 设置为 Right Detail 并且我还将单元格标识符设置为 MyCellId.
这是我使用的代码:
裸体TableViewController
partial class NakedTableViewController : UITableViewController
{
public NakedTableViewController (IntPtr handle) : base (handle)
{
TableView.RegisterClassForCellReuse (typeof(UITableViewCell), NakedTableViewSource.MyCellId);
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
TableView.Source = new NakedTableViewSource ();
}
}
裸TableViewSource
public class NakedTableViewSource : UITableViewSource
{
public static readonly NSString MyCellId = new NSString ("MyCellId");
public NakedTableViewSource ()
{
}
public override nint NumberOfSections (UITableView tableView)
{
// TODO: return the actual number of sections
return 1;
}
public override nint RowsInSection (UITableView tableview, nint section)
{
// TODO: return the actual number of items in the section
return 1;
}
public override string TitleForHeader (UITableView tableView, nint section)
{
return "Header";
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell (MyCellId) as UITableViewCell;
// if (cell == null)
// cell = new UITableViewCell ();
// TODO: populate the cell with the appropriate data based on the indexPath
cell.TextLabel.Text = "TextLabel";
cell.DetailTextLabel.Text = "DetailTextLabel";
return cell;
}
如您所见,我正在使用较新的单元重用模式。
当我尝试设置 DetailTextLabel
:
时应用程序崩溃
Object reference not set to an instance of an object
看来样式UITableViewCellStyleValue1
没有正确注册,detail标签设置失败。我做错了什么?
编辑:
堆栈跟踪:
at TestNakedTableView.NakedTableViewSource.GetCell (UIKit.UITableView tableView, Foundation.NSIndexPath indexPath) [0x00028] in /Users/some-user/Projects/TestNakedTableView/TestNakedTableView/NakedTableViewSource.cs:47
at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:62
at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:46
at TestNakedTableView.Application.Main (System.String[] args) [0x00008] in /Users/some-user/Projects/TestNakedTableView/TestNakedTableView/Main.cs:17
更新答案:
解决方案:
[Export("initWithStyle:reuseIdentifier:")]
public MyCustomCell(UITableViewCellStyle style, NSString cellIdentifier)
: base(SomeConstantValue, UITableViewCellStyle.Value1, cellIdentifier)
{
}
来源:https://forums.xamarin.com/discussion/comment/54047/#Comment_54047
上一个答案:
我认为您只是缺少初始化程序
if (cell == null) {
cell = new UITableViewCell (UITableViewCellStyle.Value1, MyCellId);
}
所以完整的代码是:
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell (MyCellId) as UITableViewCell;
if (cell == null) {
cell = new UITableViewCell (UITableViewCellStyle.Value1, MyCellId);
}
cell.TextLabel.Text = "TextLabel";
cell.DetailTextLabel.Text = "DetailTextLabel";
return cell;
}
[1] http://developer.xamarin.com/recipes/ios/content_controls/tables/specify_the_cell_type/
我想创建一个简单的示例,我想在其中使用另一个 UITableViewCellStyle
。在我的示例中,它是 UITableViewCellStyleValue1
。我在故事板中添加了一个 UITableViewController
,将 class 名称设置为 NakedTableViewController,从而使用 Dynamic Prototypes as Content,然后我将 Table View Cell Style 设置为 Right Detail 并且我还将单元格标识符设置为 MyCellId.
这是我使用的代码:
裸体TableViewController
partial class NakedTableViewController : UITableViewController
{
public NakedTableViewController (IntPtr handle) : base (handle)
{
TableView.RegisterClassForCellReuse (typeof(UITableViewCell), NakedTableViewSource.MyCellId);
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
TableView.Source = new NakedTableViewSource ();
}
}
裸TableViewSource
public class NakedTableViewSource : UITableViewSource
{
public static readonly NSString MyCellId = new NSString ("MyCellId");
public NakedTableViewSource ()
{
}
public override nint NumberOfSections (UITableView tableView)
{
// TODO: return the actual number of sections
return 1;
}
public override nint RowsInSection (UITableView tableview, nint section)
{
// TODO: return the actual number of items in the section
return 1;
}
public override string TitleForHeader (UITableView tableView, nint section)
{
return "Header";
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell (MyCellId) as UITableViewCell;
// if (cell == null)
// cell = new UITableViewCell ();
// TODO: populate the cell with the appropriate data based on the indexPath
cell.TextLabel.Text = "TextLabel";
cell.DetailTextLabel.Text = "DetailTextLabel";
return cell;
}
如您所见,我正在使用较新的单元重用模式。
当我尝试设置 DetailTextLabel
:
Object reference not set to an instance of an object
看来样式UITableViewCellStyleValue1
没有正确注册,detail标签设置失败。我做错了什么?
编辑:
堆栈跟踪:
at TestNakedTableView.NakedTableViewSource.GetCell (UIKit.UITableView tableView, Foundation.NSIndexPath indexPath) [0x00028] in /Users/some-user/Projects/TestNakedTableView/TestNakedTableView/NakedTableViewSource.cs:47
at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:62
at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:46
at TestNakedTableView.Application.Main (System.String[] args) [0x00008] in /Users/some-user/Projects/TestNakedTableView/TestNakedTableView/Main.cs:17
更新答案:
解决方案:
[Export("initWithStyle:reuseIdentifier:")]
public MyCustomCell(UITableViewCellStyle style, NSString cellIdentifier)
: base(SomeConstantValue, UITableViewCellStyle.Value1, cellIdentifier)
{
}
来源:https://forums.xamarin.com/discussion/comment/54047/#Comment_54047
上一个答案:
我认为您只是缺少初始化程序
if (cell == null) {
cell = new UITableViewCell (UITableViewCellStyle.Value1, MyCellId);
}
所以完整的代码是:
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell (MyCellId) as UITableViewCell;
if (cell == null) {
cell = new UITableViewCell (UITableViewCellStyle.Value1, MyCellId);
}
cell.TextLabel.Text = "TextLabel";
cell.DetailTextLabel.Text = "DetailTextLabel";
return cell;
}
[1] http://developer.xamarin.com/recipes/ios/content_controls/tables/specify_the_cell_type/