简体   繁体   中英

Use different cell style in Xamarin.iOS

I want to create a simple example, where I want to use another UITableViewCellStyle . In my example it is UITableViewCellStyleValue1 . I added a UITableViewController to the storyboard, set the class name to NakedTableViewController , thereby using Dynamic Prototypes as Content , then I set the Table View Cell Style to Right Detail and I also set the cell identifier to MyCellId .

This is the code I use:

NakedTableViewController

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 ();
    }
}

NakedTableViewSource

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;
    }

As you can see I'm using the newer cell reuse pattern.

The app crashes when I try to set the DetailTextLabel :

Object reference not set to an instance of an object

So it seems the style UITableViewCellStyleValue1 is not correctly registered and the setting of the detail label fails. What I'm doing wrong?

Edit:

StackTrace:

  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 

Updated Answer:

Solution:

[Export("initWithStyle:reuseIdentifier:")]
public MyCustomCell(UITableViewCellStyle style, NSString cellIdentifier)
        : base(SomeConstantValue, UITableViewCellStyle.Value1, cellIdentifier)
{

}

Source: https://forums.xamarin.com/discussion/comment/54047/#Comment_54047

Previous Answer:

I think you are just missing the initialiser

if (cell == null) {
    cell = new UITableViewCell (UITableViewCellStyle.Value1, MyCellId);
}

So the full code would be:

 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM