Prad
Prad

Reputation: 469

Xamarin C# Two UITableViewCells in one UITableView

I am trying to put two different UITableViewCell's into one UITableView however when I try to load data into a TableSource It only allows me to send one List<> at a time. However I need two List<>'s for both my Cells to display. The two Lists in my TableSource.cs class are public variables called instaData and faceData. When I run Individual requests for getting instaData and faceData it works flawlessly. Better shown then explained:

Request for InstaData

var client = new RestClient ("https://api.instagram.com/v1");
    client.ExecuteAsync (request, response => {
        var rootObject = JsonConvert.DeserializeObject<RootObject> (response.Content);
        InstagramObject.next_url = rootObject.pagination.next_url.ToString();
        FLDTRequest.instagramDataCopy = rootObject.data;
        table.InvokeOnMainThread (() => {
        table.Source = new TableSource(stream);
        ((TableSource)table.Source).instaData = rootObject.data;
        table.ReloadData ();
        });
    });

Request for FaceData

var client = new RestClient ("https://graph.facebook.com/");
                client.ExecuteAsync (request, response => {
                    var rootObject = JsonConvert.DeserializeObject<Floadt.Core.Facebook.RootObject> (response.Content);
                    FLDTRequest.facebookDataCopy = rootObject.data;
                    table.InvokeOnMainThread (() => {
                        table.Source = new TableSource(stream);
                        ((TableSource)table.Source).faceData = rootObject.data;
                        table.ReloadData ();
                    });
                });

Basically I call both methods when I want to get both data but I usually get a error from the Method I call last saying that that Object is not a reference. For Example: If i call faceData request last it would say Object is not a reference. Here is my TableSource Class:

public class TableSource : UITableViewSource 
{
    StreamViewController controller;

    public List<Datum> instaData { get; set; }
    public List<string> twitData { get; set; }
    public List<Floadt.Core.Facebook.Datum> faceData { get; set; }

    public TableSource(StreamViewController stream)
    {
        controller = stream;
    }

    public override int RowsInSection (UITableView tableview, int section)
    {
        return faceData.Count;
    }

    public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
    {
        if (indexPath.Row % 2 == 0) {
            return 340;
        } else {
            return 436;
            //return 210;
        }
    }

    public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
    {
        if (indexPath.Row % 2 == 0) {
            NetworkCheck netCheck = new NetworkCheck ();
            netCheck.runCheck ();
            // bool log = netCheck.anyLoggedIn ();

            if (tableView.ContentSize.Height - UIScreen.MainScreen.Bounds.Height <= tableView.ContentOffset.Y) {
                BTProgressHUD.Show ("Getting more...");
                FLDTRequest.getInstagramNextPage (tableView);
                BTProgressHUD.Dismiss ();
            }

            var cell = tableView.DequeueReusableCell (InstagramCell.Key) as InstagramCell;

            if (cell == null) {
                cell = new InstagramCell ();
                var views = NSBundle.MainBundle.LoadNib ("InstagramCell", cell, null);
                cell = Runtime.GetNSObject (views.ValueAt (0)) as InstagramCell;
            }

            //Datum h = instaData [indexPath.Row/2];
            //cell.BindData (h);

            return cell;

        } else {
            NetworkCheck netCheck = new NetworkCheck ();
            netCheck.runCheck ();
            // bool log = netCheck.anyLoggedIn ();

            if (tableView.ContentSize.Height - UIScreen.MainScreen.Bounds.Height <= tableView.ContentOffset.Y) {
                BTProgressHUD.Show ("Getting more...");
                FLDTRequest.getInstagramNextPage (tableView);
                BTProgressHUD.Dismiss ();
            }


            var cell = tableView.DequeueReusableCell (FacebookCell.Key) as FacebookCell;

            if (cell == null) {
                cell = new FacebookCell ();
                var views = NSBundle.MainBundle.LoadNib ("FacebookCell", cell, null);
                cell = Runtime.GetNSObject (views.ValueAt (0)) as FacebookCell;
            }

            var fbPhotoCell = tableView.DequeueReusableCell (FacebookPhotoCell.Key) as FacebookPhotoCell;

            if (fbPhotoCell == null) {
                fbPhotoCell = new FacebookPhotoCell ();
                var views = NSBundle.MainBundle.LoadNib ("FacebookPhotoCell", cell, null);
                fbPhotoCell = Runtime.GetNSObject (views.ValueAt (0)) as FacebookPhotoCell;
            }

            Floadt.Core.Facebook.Datum f = faceData [indexPath.Row/2];

            fbPhotoCell.BindData (f);
            return fbPhotoCell;
        }   
    }
}

Upvotes: 1

Views: 1839

Answers (2)

Alvin George
Alvin George

Reputation: 14296

1)Drag and Drop UITableView to ViewController and add constraints.

2)Add two Prototype cells and Associate with Two UITableViewCells.

3) Set up UITableViewSource(Combines UITableVIewDataSource and UITableViewDelegate).

    public override void ViewDidLoad ()
            {
                base.ViewDidLoad ();
                // Perform any additional setup after loading the view, typically from a nib.
                this.sampleTableView.Source = new SampleTableViewSource ();     
            }

Add Source class to ViewController.

//Table Source
        public class SampleTableViewSource : UITableViewSource
        {
            string CellIdentifier = "sampleTableViewCellID";
            string CellIdentifier2 = "sampleTableViewCell2ID";

            public override nint RowsInSection(UITableView tableview, nint section)
            {
                return 2;
            }
            public override nint NumberOfSections (UITableView tableView)
            {
                return 1;
            }
            public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
            {
                UITableViewCell cell = new UITableViewCell ();

                //---- if there are no cells to reuse, create a new one

                if (indexPath.Row == 0)
                    {
                    cell = tableView.DequeueReusableCell (CellIdentifier) as SampleTableViewCell;               
                    }
                    else if(indexPath.Row == 1 ) {
                    cell = tableView.DequeueReusableCell (CellIdentifier2) as sampleTableViewCell2;
                    }

                return cell;
            }
            public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
            {
                tableView.DeselectRow (indexPath, true);
        } 
        }

Upvotes: 0

TwoRedCells
TwoRedCells

Reputation: 116

It looks like you are trying to set table.Source twice, once for each List? You need to merge your lists into a single data source, and create a UITableViewCell that can visualize both data types.

Upvotes: 1

Related Questions