Xamarin Table View Crash

571 views Asked by At

I want to use UITableView on XamarinApp.

I tried UITableView example Populating a Table with Data ,but it doesn't work.

When i used this.Add(table); cause crash. When I remove this.Add(table) it's shows empty table.

Please help me...

Here is my code

using Foundation;
using System;
using System.CodeDom.Compiler;
using UIKit;

namespace KUkyuko
{
    partial class MyTableViewSource : UITableView
    {
        public MyTableViewSource(IntPtr handle) : base (handle)
        {
            var table = this;
            string[] tableItems = new string[] {"Vegetables","Fruits","Flower Buds","Legumes","Bulbs","Tubers"};
            table.Source = new TableSource(tableItems);
            this.Add(table); //this code cause crash
        }
    }
    public class TableSource : UITableViewSource {

        string[] TableItems;
        string CellIdentifier = "TableCell";

        public TableSource (string[] items)
        {
            TableItems = items;
        }

        public override nint RowsInSection (UITableView tableview, nint section)
        {
            return TableItems.Length;
        }

        public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
        {
            UITableViewCell cell = tableView.DequeueReusableCell (CellIdentifier);
            string item = TableItems[indexPath.Row];

            //---- if there are no cells to reuse, create a new one
            if (cell == null)
            { cell = new UITableViewCell (UITableViewCellStyle.Default, CellIdentifier); }

            cell.TextLabel.Text = item;

            return cell;
        }
    }
}
1

There are 1 answers

0
Iain Smith On BEST ANSWER

Im abit unsure as to what you trying to achieve but

I would change the tableview to a tableViewController like so::

partial class TableViewController : UITableViewController
{
    public TableViewController (IntPtr handle) : base (handle)
    {
        // Register the TableView's data source
        string[] tableItems = new string[] {"Vegetables","Fruits","Flower Buds","Legumes","Bulbs","Tubers"};
        this.TableView.Source = new TableSource(tableItems);
    }
}

public class TableSource : UITableViewSource {

    string[] TableItems;
    string CellIdentifier = "TableCell";

    public TableSource (string[] items)
    {
        TableItems = items;
    }

    public override nint RowsInSection (UITableView tableview, nint section)
    {
        return TableItems.Length;
    }

    public override nint NumberOfSections (UITableView tableView)
    {
        // TODO: return the actual number of sections
        return 1;
    }

    public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
    {
        UITableViewCell cell = tableView.DequeueReusableCell (CellIdentifier);
        string item = TableItems[indexPath.Row];

        //---- if there are no cells to reuse, create a new one
        if (cell == null)
        { cell = new UITableViewCell (UITableViewCellStyle.Default, CellIdentifier); }

        cell.TextLabel.Text = item;

        return cell;
    }
}

If you are using the table inside a viewController then you need to do this part in your viewController and set up a outlet for the table.

The reason this code crashes as you are adding this to this:

 var table = this;
 string[] tableItems = new string[] {"Vegetables","Fruits","Flower Buds","Legumes","Bulbs","Tubers"};
 table.Source = new TableSource(tableItems);
 this.Add(table); //this code cause crash as it is the same as this.Add(this)

Hope this helps!