[英]Loading uitableview
I have a uitableview with a button to clear the line. 我有一个uitableview和一个按钮来清除行。 But after I delete the database record uitableview I want to reload, but I get an error (System.NullReferenceException). 但是在删除数据库记录uitableview之后,我想重新加载,但是出现错误(System.NullReferenceException)。 I think this happens because the class inherits (UITableViewSource) and not (UIViewController). 我认为发生这种情况是因为该类继承(UITableViewSource)而不是(UIViewController)。 How to solve this? 如何解决呢?
Below is the class code responsible for loading the uitableview. 以下是负责加载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: 创建源时,请传递对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. 如果您想要的只是删除行后重新加载Uitableview
(如问题中所述),则无需实例化新的ViewController并将其再次添加到NavigationController。 There is a ReloadData
method in UitableviewSource
class which will reload the whole UITableView data. UitableviewSource
类中有一个ReloadData
方法,它将重新加载整个UITableView数据。
You may just edit the Clicked event handler as below 您可以按如下所示编辑Clicked事件处理程序
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() ;
} } };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.