简体   繁体   中英

Loading uitableview

I have a uitableview with a button to clear the line. But after I delete the database record uitableview I want to reload, but I get an error (System.NullReferenceException). I think this happens because the class inherits (UITableViewSource) and not (UIViewController). How to solve this?

Below is the class code responsible for loading the uitableview.

Thank you very much

public class FonteTabelaExercicios : UITableViewSource
{
    private ExercicioBanco banco = new ExercicioBanco ();
    private List<Exercicio> exercicosBanco;
    private string cellIdentifier = "TableCell";
    UIButton btn;
    private NavController nav = new NavController ();

    public FonteTabelaExercicios (List<Exercicio> banco)
    {
        exercicosBanco = banco;
    }

    public override nint RowsInSection (UITableView tableview, nint section)
    {
        if (exercicosBanco != null) {
            return exercicosBanco.Count;
        } else {
            return 0;
        }
    }

    public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath)
    {
        UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier); 
        if (cell == null)
            cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);

        cell.TextLabel.Text = exercicosBanco [indexPath.Row].Nome + " - " + exercicosBanco [indexPath.Row].Quantidade;

        btn                 = new UIButton (new CGRect (0, 0, 70, 30));
        btn.SetTitle("Apagar", UIControlState.Normal);
        btn.SetTitleColor (UIColor.White,UIControlState.Normal);
        btn.BackgroundColor = UIColor.Blue;
        cell.AccessoryView  = btn;

        btn.TouchUpInside += (sender, e) => {

            int result = 0;

            UIAlertView alert = new UIAlertView(
                "Confirmação", 
                "Deseja apagar este dado?", 
                null, 
                NSBundle.MainBundle.LocalizedString ("Cancelar", "Cancel"),
                NSBundle.MainBundle.LocalizedString ("OK", "OK")
            );

            alert.Show ();
            alert.Clicked += (object sender2, UIButtonEventArgs es) => { 
                if (es.ButtonIndex == 0 ) 
                {
                    Console.WriteLine("Não");
                }
                else
                {
                    result = banco.ExecutaDelete(exercicosBanco [indexPath.Row]);
                    if (result > 0) {
                        //Loads listaRecorsViewController with uitableview
                        listaRecorsViewController record = (listaRecorsViewController)nav.Storyboard.InstantiateViewController("listaRecords");
                        nav.PushViewController(record, false);
                    }
                }
            };
        };

        return cell;
    }
}

When you create your source, pass in a reference to the TableViewController:

private TableCiewController parent;
public FonteTabelaExercicios (TableViewController parent, List<Exercicio> banco)
{
    exercicosBanco = banco;
    this.parent = parent;
}

then, when you need to perform a navigation operation

parent.NavigationController.PushViewController(record,false);

If what you want is to just reload the Uitableview after deleting the row (as explained in the question) you need not instantiate a new ViewController and add it to NavigationController again. There is a ReloadData method in UitableviewSource class which will reload the whole UITableView data.

You may just edit the Clicked event handler as below

alert.Clicked += (object sender2, UIButtonEventArgs es) => { if (es.ButtonIndex == 0 ) { Console.WriteLine("Não"); } 
else { result = banco.ExecutaDelete(exercicosBanco [indexPath.Row]); 
if (result > 0) { 
exercicosBanco.RemoveAt(indexPath.Row);
ReloadData() ;
} } };

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