[英]Loading uitableview
我有一個uitableview和一個按鈕來清除行。 但是在刪除數據庫記錄uitableview之后,我想重新加載,但是出現錯誤(System.NullReferenceException)。 我認為發生這種情況是因為該類繼承(UITableViewSource)而不是(UIViewController)。 如何解決呢?
以下是負責加載uitableview的類代碼。
非常感謝你
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;
}
}
創建源時,請傳遞對TableViewController的引用:
private TableCiewController parent;
public FonteTabelaExercicios (TableViewController parent, List<Exercicio> banco)
{
exercicosBanco = banco;
this.parent = parent;
}
然后,當您需要執行導航操作時
parent.NavigationController.PushViewController(record,false);
如果您想要的只是刪除行后重新加載Uitableview
(如問題中所述),則無需實例化新的ViewController並將其再次添加到NavigationController。 UitableviewSource
類中有一個ReloadData
方法,它將重新加載整個UITableView數據。
您可以按如下所示編輯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.