Table view is not showing data

I have a table view and it is not showing my data. my data is Schedule class.
I have this class :
public class ScheduleTableViewSource : UITableViewSource
{
private Schedule[] school;

    public ScheduleTableViewSource(Schedule[] school)
    {
        this.school = school;
    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        var cell = new UITableViewCell(UITableViewCellStyle.Default, "");
        cell.TextLabel.Text = Convert.ToString(school[indexPath.Row]);
        return cell;
    }

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

and this is how I use it :
partial void Get_button_TouchUpInside(UIButton sender)
{

        bool check = NetworkInterface.GetIsNetworkAvailable();
        if(check)
        {

            Service1 client = new Service1();
            var school = client.CypressRidge("CypressRidge");
            SchoolSchedule.Source = new ScheduleTableViewSource(school);

        }
        else{
            Console.Write("Error no Internet connection");
            return;
        }
     //   int stop =0;

    }

the constructor is called but not the other two functions and table is blank.

Answers

  • FinHorsleyFinHorsley USMember ✭✭

    is SchoolSchedule the name of your TableView?

    If it is not then you have not set the source of the TableView. Set your source by using

    TableView.Source = new ScheduleTableViewSource()

    and then remember to reload the table TableView.ReloadData()

  • BrandonBooneBrandonBoone USMember ✭✭

    it is and I tried that :

       partial void Get_button_TouchUpInside(UIButton sender)
        {
    
            bool check = NetworkInterface.GetIsNetworkAvailable();
            if(check)
            {
    
                Service1 client = new Service1();
                var school = client.CypressRidge("CypressRidge");
                SchoolSchedule.Source = new ScheduleTableViewSource(school);
                SchoolSchedule.ReloadData();
    
            }
            else{
                Console.Write("Error no Internet connection");
                return;
            }
         //   int stop =0;
    
        }
    

    but it does nothing

    public override nint RowsInSection(UITableView tableview, nint section)
    {
    return school.Length;
    }
    is called
    but

    partial void Get_button_TouchUpInside(UIButton sender)
    {

            bool check = NetworkInterface.GetIsNetworkAvailable();
            if(check)
            {
    
                Service1 client = new Service1();
                var school = client.CypressRidge("CypressRidge");
                SchoolSchedule.Source = new ScheduleTableViewSource(school);
                SchoolSchedule.ReloadData();
    
            }
            else{
                Console.Write("Error no Internet connection");
                return;
            }
         //   int stop =0;
    
        }
    

    is not called

  • BrandonBooneBrandonBoone USMember ✭✭

    anyone ?

  • FinHorsleyFinHorsley USMember ✭✭

    Can you attach your code as a zip file to the chat, I'll have a look at it?

  • BrandonBooneBrandonBoone USMember ✭✭

    here

  • BrandonBooneBrandonBoone USMember ✭✭

    did you have time to look at it ?

  • FinHorsleyFinHorsley USMember ✭✭

    Sorry that took so long! Here is a list of problems I think I have identified.

    Side note - I never use storyboards and instead create views in code (find the Xamarin editor too buggy), so just take this into account when I'm offering advice regarding views created with the storyboard.


    • 1. Nothing wrong with your ScheduleTableViewSource

    I created a new ViewController with a TableView (identical to your ViewController) in code, and found that there was nothing wrong with your ScheduleTableViewSource. I was able to get data from the Service1() client and display it in my tableView. GetCell was called!

    Only thing I had to do was use a default UITableViewCell instead of your ````ScheduleCell``, although see point 3 for for detail.

    Important

    Always register your cells for reuse before dequeueing them!
    tableView.RegisterClassForCellReuse(typeof(ScheduleCell), "cell_id");


    • 2. Error with your SchoolSchedule TableView

    I think that there is something wrong with your tableView that you created. I read a number of articles that said there seemed to be a bug within Xamarin that meant tableviews didnt display any data (worth doing some of your own research on this). I would suggest deleting it from your storyboard file, and then adding a new tableview (remember to add constraints so that it scales to fit different sized devices).


    • 3. Error with your ScheduleCell

    Using my own ViewController as a test platform, I then tried to present a tableview using your ScheduleCell. I dequeued the cell, and then called UpdateCell() however all I got was NullReferenceExceptions! Might be a good idea to delete the cell and create it again (delete the ScheduleCell .cs and .designer.cs files as well). You can do all this using the Xcode editor, as its just easier to use (right click the storyboard file and select "Open with Xcode).


    • 4. Creating Views Programatically!

    It took me a year before I first started creating views in code, but once you have learnt the basics, its actually pretty easy! I can send you over a demo if you'd like, using your project you sent me as a basis.

    Creating views in code gives you a much better understanding of what is going on under the hood, and gets you on the path to developing some really great looking apps (e.g. adding views underneath your navigation bar - see Youtube's previous version app).

    Also removes the pain that is adding constraints to views in the Xamarin storyboard.


  • BrandonBooneBrandonBoone USMember ✭✭
    edited December 2017

    where you able to get the program to work for you ?
    can you attach it ?

  • FinHorsleyFinHorsley USMember ✭✭

    what do you mean by that first question > @BrandonBoone said:

    where you able to get the program to work for you ?

  • BrandonBooneBrandonBoone USMember ✭✭

    I meant were you able to view data in the table ? did the code work for you ?I tried to deleted ScheduleCell and it was a bad idea now I cant get that part to work again. I had to make a new one called ScheduleCell2 but it will not see the 3 labels and I keep getting errors.

  • FinHorsleyFinHorsley USMember ✭✭

    ScheduleCell - didn't work
    SceduleCellTableViewSource - works
    SchoolSchedule tableview - didn't work

    want to upload your solution again?

  • BrandonBooneBrandonBoone USMember ✭✭

    so it is not working for you too ?

  • BrandonBooneBrandonBoone USMember ✭✭

    here it is

  • FinHorsleyFinHorsley USMember ✭✭

    the code you send me did not run, see my long explanation further up.

  • BrandonBooneBrandonBoone USMember ✭✭

    yeah I wanted to know if you where able to fix it. idk why it is not working

  • BrandonBooneBrandonBoone USMember ✭✭

    are you able to use tables ?

  • BrandonBooneBrandonBoone USMember ✭✭

    ok so you are right about delete the table and making a new one...
    but that class ScheduleCell will just not work... and i cant make a new one ...

  • FinHorsleyFinHorsley USMember ✭✭

    Sorry about the long wait, had busy couple of days.

    What do you mean by 'cant make a new one' - are you struggling to create a new ScheduleCell in the Storyboard, or is there an issue somewhere else?

    Check if you have removed ScheduleCell (the one that wasn't working and you deleted)

    Make sure that you have deleted both the ScheduleCell.cs and the .designer.cs file from the project, otherwise you will run into issue with naming the replacement cell. To keep things simple you might want to just call the new cell 'NewScheduleCell' , for example.

  • FinHorsleyFinHorsley USMember ✭✭

    any luck?

  • BrandonBooneBrandonBoone USMember ✭✭

    yeah I ended up deleting a lot of stuff and starting over.

Sign In or Register to comment.